├── .gitignore ├── .gitattributes ├── tawk_to.info ├── composer.json ├── js ├── tawk_to.widget.js └── tawk_to.admin.js ├── css └── tawk_to.admin.css ├── includes └── tawk_to.helper.php ├── composer.lock ├── README.md ├── tawk_to.module └── tawk_to.admin.inc /.gitignore: -------------------------------------------------------------------------------- 1 | docker/bin 2 | /vendor/ 3 | /dev 4 | /tmp 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # exclude docker files from archive 2 | /docker export-ignore 3 | /.github export-ignore 4 | -------------------------------------------------------------------------------- /tawk_to.info: -------------------------------------------------------------------------------- 1 | name = Tawk.to 2 | description = Tawk.to 3 | core = 7.x 4 | configure = admin/config/tawk 5 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tawk/tawk-drupal", 3 | "description": "tawk.to Live Chat module for Drupal 7 CMS", 4 | "type": "project", 5 | "license": "GPLv3", 6 | "require": { 7 | "tawk/url-utils": "2.0.1" 8 | }, 9 | "repositories": { 10 | "tawk-url-utils": { 11 | "type": "vcs", 12 | "url": "https://github.com/tawk/tawk-url-utils.git" 13 | } 14 | }, 15 | "scripts": { 16 | "build:prod": "composer install --no-dev", 17 | "package": "composer run clean && mkdir -p ./tmp/tawktodrupal7 && cp -r ./css ./tmp/tawktodrupal7 && cp -r ./includes ./tmp/tawktodrupal7 && cp -r ./js ./tmp/tawktodrupal7 && cp -r ./vendor ./tmp/tawktodrupal7 && cp ./tawk_to.* ./tmp/tawktodrupal7 && cp README.md ./tmp/tawktodrupal7 && (cd ./tmp && zip -9 -rq ./tawktodrupal7.zip ./tawktodrupal7)", 18 | "clean": "rm -rf ./tmp" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /js/tawk_to.widget.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | jQuery(document).ready(function() { 4 | var tawktoSettings = Drupal.settings.tawkto || {}; 5 | 6 | window.Tawk_LoadStart = new Date(); 7 | window.Tawk_API = window.Tawk_API || {}; 8 | 9 | if (tawktoSettings.user && tawktoSettings.enableVisitorRecognition) { 10 | window.Tawk_API.visitor = { 11 | name : tawktoSettings.user.name, 12 | email : tawktoSettings.user.email 13 | }; 14 | } 15 | 16 | var curpage = window.location.href; 17 | var hash = window.location.hash; 18 | window.Tawk_API.onLoad = function() { 19 | if (curpage.indexOf('admin/') !== -1 || hash.indexOf('overlay') !== -1) { 20 | window.Tawk_API.hideWidget(); 21 | } 22 | }; 23 | 24 | (function() { 25 | var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0]; 26 | s1.async=true; 27 | s1.src="https://embed.tawk.to/" + tawktoSettings.pageId + "/" + tawktoSettings.widgetId; 28 | s1.charset="UTF-8"; 29 | s1.setAttribute("crossorigin","*"); 30 | s0.parentNode.insertBefore(s1,s0); 31 | })(); 32 | }); 33 | -------------------------------------------------------------------------------- /css/tawk_to.admin.css: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * Removes styling from html in admin.inc.php 4 | * 5 | * @package tawk.to module for Drupal CMS 6 | * @copyright (C) 2021 tawk.to 7 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html 8 | */ 9 | 10 | /** 11 | * Notification 12 | * Initially hide the status message. Jquery is used to show after updating settings 13 | */ 14 | .success-message-container { 15 | display: none; 16 | } 17 | 18 | /* iframe */ 19 | .tawkto-property-widget-selection { 20 | min-height: 275px; 21 | width: 100%; 22 | border: none; 23 | margin: 5px 0; 24 | } 25 | 26 | /* Tooltip */ 27 | .tooltip { 28 | position: relative; 29 | display: inline; 30 | color: #03a84e; 31 | } 32 | 33 | .tooltip .tooltiptext { 34 | visibility: hidden; 35 | background-color: #545454; 36 | color: #fff; 37 | text-align: center; 38 | padding: 0.5rem; 39 | max-width: 300px; 40 | border-radius: 0.5rem; 41 | font-size: 0.7rem; 42 | line-height: 0.9; 43 | 44 | /* Position the tooltip text - see examples below! */ 45 | position: absolute; 46 | z-index: 1000; 47 | top: 12px; 48 | } 49 | 50 | .tooltip .tooltiptext::before { 51 | content: ""; 52 | display: block; 53 | width: 0; 54 | height: 0; 55 | position: absolute; 56 | border-left: 5px solid transparent; 57 | border-right: 5px solid transparent; 58 | border-bottom: 5px solid #545454; 59 | top: -5px; 60 | left: 5px; 61 | } 62 | 63 | .tooltip:hover .tooltiptext { 64 | visibility: visible; 65 | } 66 | -------------------------------------------------------------------------------- /includes/tawk_to.helper.php: -------------------------------------------------------------------------------- 1 | variable_get(TawkHelper::TAWK_TO_PAGE_ID, ''), 21 | 'widget_id' => variable_get(TawkHelper::TAWK_TO_WIDGET_ID, ''), 22 | ); 23 | } 24 | 25 | /** 26 | * Checks if the current user is the same user that selected the widget 27 | */ 28 | public static function check_same_user($current_user) { 29 | $saved_user = variable_get(TawkHelper::TAWK_TO_WIDGET_USER_ID, ''); 30 | 31 | if (empty($saved_user)) { 32 | return false; 33 | } 34 | 35 | return $current_user === $saved_user; 36 | } 37 | 38 | /** 39 | * Base url for tawk.to application which serves iframe 40 | */ 41 | public static function get_base_url() { 42 | return 'https://plugins.tawk.to'; 43 | } 44 | 45 | /** 46 | * Constructs url for configuration iframe 47 | */ 48 | public static function get_iframe_url() { 49 | $widget = TawkHelper::get_widget(); 50 | 51 | if (!$widget) { 52 | $widget = array( 53 | 'page_id' => '', 54 | 'widget_id' => '', 55 | ); 56 | } 57 | return TawkHelper::get_base_url() . '/generic/widgets?currentWidgetId=' . $widget['widget_id'] . '¤tPageId=' . $widget['page_id'] . '&pltf=drupal'; 58 | } 59 | 60 | /** 61 | * Gets the module path 62 | */ 63 | public static function get_module_path() { 64 | return drupal_get_path('module', 'tawk_to'); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "d4af42901445724f19e91af2ad94628a", 8 | "packages": [ 9 | { 10 | "name": "tawk/url-utils", 11 | "version": "2.0.1", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/tawk/tawk-url-utils.git", 15 | "reference": "73c166333707d893b0160fa9b5eae7aa8fbfa03c" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/tawk/tawk-url-utils/zipball/73c166333707d893b0160fa9b5eae7aa8fbfa03c", 20 | "reference": "73c166333707d893b0160fa9b5eae7aa8fbfa03c", 21 | "shasum": "" 22 | }, 23 | "require-dev": { 24 | "phpcompatibility/php-compatibility": "^9.3", 25 | "phpunit/phpunit": "^9.5", 26 | "squizlabs/php_codesniffer": "^3.6" 27 | }, 28 | "type": "library", 29 | "autoload": { 30 | "psr-4": { 31 | "Tawk\\": "lib", 32 | "Tawk\\Tests\\": "tests" 33 | } 34 | }, 35 | "scripts": { 36 | "post-install-cmd": [ 37 | "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths vendor/phpcompatibility/php-compatibility)" 38 | ], 39 | "post-update-cmd": [ 40 | "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths vendor/phpcompatibility/php-compatibility)" 41 | ], 42 | "test": [ 43 | "phpunit" 44 | ], 45 | "lint": [ 46 | "phpcs -p -s -v --runtime-set ignore_warnings_on_exit true ." 47 | ], 48 | "lint:fix": [ 49 | "phpcbf -p -s -v .; err=$?; if [ $err -eq 1 ]; then exit 0; else exit $err; fi;" 50 | ] 51 | }, 52 | "support": { 53 | "source": "https://github.com/tawk/tawk-url-utils/tree/2.0.1", 54 | "issues": "https://github.com/tawk/tawk-url-utils/issues" 55 | }, 56 | "time": "2022-01-28T11:14:45+00:00" 57 | } 58 | ], 59 | "packages-dev": [], 60 | "aliases": [], 61 | "minimum-stability": "stable", 62 | "stability-flags": [], 63 | "prefer-stable": false, 64 | "prefer-lowest": false, 65 | "platform": [], 66 | "platform-dev": [], 67 | "plugin-api-version": "2.1.0" 68 | } 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tawk.to Live Chat 2 | 3 | Free live chat widget for your site 4 | 5 | *** 6 | **This module will no longer receive updates due to [EOL of Drupal 7](https://www.drupal.org/about/drupal-7/d7eol/partners). It is highly recommended to migrate your site to Drupal 8 or later and use the [tawk.to Drupal 8 module](https://www.drupal.org/project/tawkto) instead.** 7 | *** 8 | 9 | ## Description 10 | The tawk.to Drupal module allows you to easily choose which one of your tawk.to widgets you want to use on your drupal page. To create an account, please visit https://tawk.to 11 | 12 | To answer chats go to https://dashboard.tawk.to/ 13 | * For a full description of the module, visit the project page https://www.drupal.org/project/tawktodrupal7 14 | * To submit bug reports and feature suggestions, or to track changes please email support@tawk.to 15 | 16 | Don't have a tawk.to account yet? [Create one here.](https://www.tawk.to/?utm_source=drupal7&utm_medium=link&utm_campaign=signup) 17 | 18 | ## Installation 19 | 20 | ### Module Installer 21 | 1. Download the `tawktodrupal7.zip` from [latest release.](https://github.com/tawk/tawk-drupal/releases). 22 | 2. Log in to the your administration panel and go to `Modules`. 23 | 3. In the modules page, click on `Install new module`. 24 | 4. Click on `Choose File`, select the downloaded module, and click on the `Install` button. 25 | 5. After installation completes, click on `Enable newly added modules` link and it'll redirect you back to the `Modules` page. 26 | 6. In the modules page, search for `tawk.to` and enable the module by checking the `Enabled` checkbox. 27 | 7. Lastly, click on `Save Configuration` button to complete the installation. 28 | 29 | ### Manual Installation 30 | 1. Download the `tawktodrupal7.zip` from [latest release.](https://github.com/tawk/tawk-drupal/releases). 31 | 2. In the extracted files, copy the contents of the `tawktodrupal7` directory to `/modules/tawkto` directory. 32 | 2. Log in to the your administration panel and go to `Modules`. 33 | 3. In the modules page, search for `tawk.to` and enable the module by checking the `Enabled` checkbox. 34 | 4. Lastly, click on `Save Configuration` button to complete the installation. 35 | 36 | ### Configuration 37 | 1. In the administration panel, go to `Modules`. 38 | 2. In the modules page, search for `tawk.to` and click on the `Configure` link to go to the configuration page. 39 | 3. Click on the `Widget` link to navigate to the widget configuration page. 40 | 4. Log in to your tawk.to account and you will be able to select which widget to display. 41 | 42 | ## Frequently Asked Questions 43 | Visit our [Help Center](https://help.tawk.to) for answers to FAQs. 44 | 45 | * After installation enable module, go to configuration and choose widget you want to use. 46 | * It requires that you have tawk.to account before you can start using it. If you don't have one, go to https://www.tawk.to/ to create one for free. 47 | -------------------------------------------------------------------------------- /js/tawk_to.admin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // initialize variables 4 | var tawktoSettings = {}; 5 | 6 | jQuery(document).ready(function() { 7 | tawktoSettings = Drupal.settings.tawkto || {}; 8 | 9 | var currentHost = window.location.protocol + "//" + window.location.host; 10 | var url = tawktoSettings.iframeUrl + "&parentDomain=" + currentHost; 11 | 12 | jQuery("#tawkIframe").attr("src", url); 13 | 14 | if (jQuery("#always_display").attr('checked')) { 15 | jQuery('.show_specific').attr('disabled', true); 16 | jQuery(".div_show_specific").hide(); 17 | } else { 18 | jQuery('.hide_specific').attr('disabled', true); 19 | jQuery(".div_hide_specific").hide(); 20 | } 21 | 22 | jQuery("#always_display").change(function() { 23 | if (this.checked) { 24 | jQuery('.hide_specific').attr('disabled', false); 25 | jQuery('.show_specific').attr('disabled', true); 26 | jQuery(".div_hide_specific").show(); 27 | jQuery(".div_show_specific").hide(); 28 | } else { 29 | jQuery('.hide_specific').attr('disabled', true); 30 | jQuery('.show_specific').attr('disabled', false); 31 | jQuery(".div_hide_specific").hide(); 32 | jQuery(".div_show_specific").show(); 33 | } 34 | }); 35 | }); 36 | 37 | window.addEventListener("message", function(e) { 38 | if (e.origin === tawktoSettings.baseUrl) { 39 | if (e.data.action === "setWidget") { 40 | setWidget(e); 41 | } 42 | 43 | if (e.data.action === "removeWidget") { 44 | removeWidget(e); 45 | } 46 | 47 | if (e.data.action === 'reloadHeight') { 48 | reloadIframeHeight(e.data.height); 49 | } 50 | } 51 | }); 52 | 53 | function setWidget(e) { 54 | jQuery.post("?q=admin/config/tawk/setwidget", { 55 | pageId : e.data.pageId, 56 | widgetId : e.data.widgetId 57 | }, function(r) { 58 | if (r.success) { 59 | jQuery('input[name="page_id"]').val(e.data.pageId); 60 | jQuery('input[name="widget_id"]').val(e.data.widgetId); 61 | jQuery('#widget_already_set').hide(); 62 | e.source.postMessage({action: "setDone"}, tawktoSettings.baseUrl); 63 | } else { 64 | e.source.postMessage({action: "setFail"}, tawktoSettings.baseUrl); 65 | } 66 | }); 67 | } 68 | 69 | function removeWidget(e) { 70 | jQuery.post("?q=admin/config/tawk/removewidget", function(r) { 71 | if (r.success) { 72 | jQuery('input[name="page_id"]').val(''); 73 | jQuery('input[name="widget_id"]').val(''); 74 | jQuery('#widget_already_set').hide(); 75 | e.source.postMessage({action: "removeDone"}, tawktoSettings.baseUrl); 76 | } else { 77 | e.source.postMessage({action: "removeFail"}, tawktoSettings.baseUrl); 78 | } 79 | }); 80 | } 81 | 82 | function reloadIframeHeight(height) { 83 | if (!height) { 84 | return; 85 | } 86 | 87 | var iframe = jQuery('#tawkIframe'); 88 | if (height === iframe.height()) { 89 | return; 90 | } 91 | 92 | iframe.height(height); 93 | } 94 | -------------------------------------------------------------------------------- /tawk_to.module: -------------------------------------------------------------------------------- 1 | enable_visitor_recognition)) { 47 | $enable_visitor_recognition = $options->enable_visitor_recognition; 48 | } 49 | 50 | // prepare visibility 51 | $current_url = $base_url.$_SERVER['REQUEST_URI']; 52 | 53 | if (false === $options->always_display) { 54 | $show_pages = json_decode($options->show_oncustom); 55 | $show = false; 56 | 57 | if (UrlPatternMatcher::match($current_url, $show_pages)) { 58 | $show = true; 59 | } 60 | 61 | if (arg(0) === 'taxonomy' && arg(1) === 'term' && is_numeric(arg(2)) && empty(arg(3))) { 62 | if (true === $options->show_oncategory) { 63 | $show = true; 64 | } 65 | } 66 | 67 | // front page 68 | if (drupal_is_front_page()) { 69 | if (true === $options->show_onfrontpage) { 70 | $show = true; 71 | } 72 | } 73 | 74 | if (!$show) { 75 | return; 76 | } 77 | } else { 78 | if (isset($options->hide_oncustom)) { 79 | $hide_pages = json_decode($options->hide_oncustom); 80 | $show = true; 81 | 82 | $current_url = (string) $current_url; 83 | 84 | if (UrlPatternMatcher::match($current_url, $hide_pages)) { 85 | $show = false; 86 | } 87 | 88 | if (!$show) { 89 | return; 90 | } 91 | } 92 | } 93 | } 94 | 95 | $widget_options = array( 96 | 'pageId' => $page_id, 97 | 'widgetId' => $widget_id, 98 | 'enableVisitorRecognition' => $enable_visitor_recognition, 99 | ); 100 | 101 | if ($user->uid && $enable_visitor_recognition) { 102 | $widget_options['user'] = array( 103 | 'name' => $user->name, 104 | 'email' => $user->mail, 105 | ); 106 | } 107 | 108 | drupal_add_js(array( 109 | 'tawkto' => $widget_options, 110 | ), 'setting'); 111 | drupal_add_js(TawkHelper::get_module_path() . '/js/tawk_to.widget.js', 'file'); 112 | 113 | return $variables; 114 | } 115 | 116 | /** 117 | * Hooks up configuration menu items and paths for ajax call endpoints. 118 | */ 119 | function tawk_to_menu() { 120 | $items = array(); 121 | $items['admin/config/tawk'] = array( 122 | 'title' => 'tawk.to', 123 | 'description' => 'tawk.to configuration', 124 | 'position' => 'left', 125 | 'weight' => -100, 126 | 'page callback' => 'system_admin_menu_block_page', 127 | 'access arguments' => array('administer site configuration'), 128 | 'file' => 'system.admin.inc', 129 | 'file path' => drupal_get_path('module', 'system'), 130 | ); 131 | 132 | $items['admin/config/tawk/widget'] = array( 133 | 'title' => 'Widget', 134 | 'description' => 'Included tawk.to widget configuration', 135 | 'page callback' => 'drupal_get_form', 136 | 'page arguments' => array('tawk_to_admin_form'), 137 | 'access arguments' => array('administer site configuration'), 138 | 'type' => MENU_NORMAL_ITEM, 139 | ); 140 | 141 | $items['admin/config/tawk/setwidget'] = array( 142 | 'title' => 'Set widget', 143 | 'page callback' => 'tawk_to_set_widget', 144 | 'access arguments' => array('administer site configuration'), 145 | 'type' => MENU_CALLBACK, 146 | ); 147 | 148 | $items['admin/config/tawk/removewidget'] = array( 149 | 'title' => 'Remove widget', 150 | 'page callback' => 'tawk_to_remove_widget', 151 | 'access arguments' => array('administer site configuration'), 152 | 'type' => MENU_CALLBACK, 153 | ); 154 | 155 | return $items; 156 | } 157 | 158 | /** 159 | * Ajax endpoint which is used to set selected widget details. 160 | */ 161 | function tawk_to_set_widget() { 162 | if (!isset($_POST['pageId']) || !isset($_POST['widgetId'])) { 163 | return drupal_json_output(array('success' => false)); 164 | } 165 | 166 | if (preg_match('/^[0-9A-Fa-f]{24}$/', $_POST['pageId']) !== 1 || preg_match('/^[a-z0-9]{1,50}$/i', $_POST['widgetId']) !== 1) { 167 | return drupal_json_output(array('success' => false)); 168 | } 169 | 170 | variable_set(TawkHelper::TAWK_TO_PAGE_ID, $_POST['pageId']); 171 | variable_set(TawkHelper::TAWK_TO_WIDGET_ID, $_POST['widgetId']); 172 | 173 | global $user; 174 | if ($user->uid) { 175 | variable_set(TawkHelper::TAWK_TO_WIDGET_USER_ID, $user->uid); 176 | } 177 | 178 | // Flush page cache so widget will appear at next page load 179 | cache_clear_all(NULL, 'cache_page'); 180 | 181 | drupal_json_output(array('success' => true)); 182 | } 183 | 184 | /** 185 | * Ajax endpoint which is used to remove currently selected widget details. 186 | */ 187 | function tawk_to_remove_widget() { 188 | variable_del(TawkHelper::TAWK_TO_PAGE_ID); 189 | variable_del(TawkHelper::TAWK_TO_WIDGET_ID); 190 | 191 | // Flush page cache so widget will disappear at next page load 192 | cache_clear_all(NULL, 'cache_page'); 193 | 194 | drupal_json_output(array('success' => true)); 195 | } 196 | -------------------------------------------------------------------------------- /tawk_to.admin.inc: -------------------------------------------------------------------------------- 1 | uid); 17 | $widget = TawkHelper::get_widget(); 18 | $base_url = TawkHelper::get_base_url(); 19 | $iframe_url = TawkHelper::get_iframe_url(); 20 | $display_opts = variable_get(TawkHelper::TAWK_TO_WIDGET_OPTIONS, ''); 21 | 22 | if ($display_opts && !empty($display_opts)) { 23 | $display_opts = json_decode($display_opts); 24 | } 25 | else { 26 | $display_opts = null; 27 | } 28 | 29 | // css and js files 30 | $module_path = TawkHelper::get_module_path(); 31 | $form['#attached']['css'][] = array( 32 | 'data' => $module_path . '/css/tawk_to.admin.css', 33 | 'type' => 'file', 34 | ); 35 | $form['#attached']['js'][] = array( 36 | 'data' => $module_path . '/js/tawk_to.admin.js', 37 | 'type' => 'file', 38 | ); 39 | 40 | $form['#attached']['js'][] = array( 41 | 'data' => array( 42 | 'tawkto' => array( 43 | 'iframeUrl' => $iframe_url, 44 | 'baseUrl' => $base_url, 45 | ), 46 | ), 47 | 'type' => 'setting', 48 | ); 49 | 50 | $form['widget_selection'] = build_widget_selection($same_user, $widget); 51 | $form['visibility_settings'] = build_visibility_opts_form($display_opts); 52 | $form['privacy_settings'] = build_privacy_settings_form($display_opts); 53 | 54 | // submit button 55 | $form['actions']['#type'] = 'actions'; 56 | $form['actions']['submit'] = array( 57 | '#type' => 'submit', 58 | '#value' => t('Save configuration'), 59 | ); 60 | 61 | $form['#submit'][] = 'tawk_to_admin_form_submit'; 62 | 63 | return $form; 64 | } 65 | 66 | /** 67 | * builds tawk.to widget selection area 68 | */ 69 | function build_widget_selection($same_user, $widget) { 70 | $form = array( 71 | '#type' => 'fieldset', 72 | '#title' => t('Property and Widget Selection'), 73 | '#description' => t('Select the property and widget from your tawk.to account') 74 | ); 75 | 76 | if(!$same_user) { 77 | $form['already_set'] = array( 78 | '#type' => 'markup', 79 | '#markup' => '
Notice: Widget already set by other user
', 80 | ); 81 | } 82 | 83 | $form['iframe'] = array( 84 | '#type' => 'markup', 85 | '#markup' => ' 86 | 87 | 88 | 89 | ', 90 | ); 91 | 92 | return $form; 93 | } 94 | 95 | /** 96 | * builds tawk.to widget visibility options form. 97 | */ 98 | function build_visibility_opts_form($display_opts) { 99 | // visibility settings 100 | $form = array( 101 | '#type' => 'fieldset', 102 | '#title' => t('Visibility Settings'), 103 | '#description' => t('Define where the tawk.to widget will and won\'t show'), 104 | ); 105 | 106 | // always display config 107 | $always_display_attr = array(); 108 | if (!is_null($display_opts) && $display_opts->always_display) { 109 | $always_display_attr['checked'] = true; 110 | } 111 | 112 | $form['always_display'] = array( 113 | '#type' => 'checkbox', 114 | '#title' => t('Always show Tawk.To widget on every page'), 115 | '#description' => t('Select to show on all except the exceptions 116 | De-select to select the specific pages'), 117 | '#id' => t('always_display'), 118 | '#attributes' => $always_display_attr, 119 | ); 120 | 121 | // exclude url config 122 | $form['hide_oncustom'] = array( 123 | '#type' => 'textarea', 124 | '#title' => t('Except on pages:'), 125 | '#description' => t(' 126 | Add URLs/paths to pages in which you would like to hide the widget.
127 | Put each URL/path in a new line. Paths should have a leading \'/\'. 128 |
129 |
130 | Examples of accepted path patterns 131 | 156 |
157 | '), 158 | '#prefix' => '
', 159 | '#suffix' => '
', 160 | '#id' => t('hide_oncustom'), 161 | '#default_value' => !is_null($display_opts) && $display_opts->hide_oncustom ? 162 | format_multiline_url_paths($display_opts->hide_oncustom) : '', 163 | ); 164 | 165 | // show on front page config 166 | $show_onfrontpage_attr = array(); 167 | if (!is_null($display_opts) && $display_opts->show_onfrontpage) { 168 | $show_onfrontpage_attr['checked'] = true; 169 | } 170 | 171 | $form['show_onfrontpage'] = array( 172 | '#type' => 'checkbox', 173 | '#title' => t('Show on front page'), 174 | '#prefix' => '
', 175 | '#suffix' => '
', 176 | '#id' => t('show_onfrontpage'), 177 | '#attributes' => $show_onfrontpage_attr, 178 | ); 179 | 180 | // show on category config 181 | $show_oncategory_attr = array(); 182 | if (!is_null($display_opts) && $display_opts->show_oncategory) { 183 | $show_oncategory_attr['checked'] = true; 184 | } 185 | 186 | $form['show_oncategory'] = array( 187 | '#type' => 'checkbox', 188 | '#title' => t('Show on category pages'), 189 | '#description' => t('Select to show on pages for category terms'), 190 | '#prefix' => '
', 191 | '#suffix' => '
', 192 | '#id' => t('show_oncategory'), 193 | '#attributes' => $show_oncategory_attr, 194 | ); 195 | 196 | // include urls config 197 | $show_oncustom_default_value = ''; 198 | if (!is_null($display_opts) && $display_opts->show_oncustom) { 199 | $show_oncustom_default_value = format_multiline_url_paths($display_opts->show_oncustom); 200 | } 201 | $form['show_oncustom'] = array( 202 | '#type' => 'textarea', 203 | '#title' => t('Show on pages:'), 204 | '#description' => t(' 205 | Add URLs/paths to pages in which you would like to hide the widget.
206 | Put each URL/path in a new line. Paths should have a leading \'/\'. 207 |
208 |
209 | Examples of accepted path patterns 210 | 235 |
236 | '), 237 | '#prefix' => '
', 238 | '#suffix' => '
', 239 | '#id' => t('show_oncustom'), 240 | '#default_value' => $show_oncustom_default_value, 241 | ); 242 | 243 | return $form; 244 | } 245 | 246 | /** 247 | * builds tawk.to privacy settings form 248 | */ 249 | function build_privacy_settings_form($display_opts) { 250 | // privacy settings 251 | $form = array( 252 | '#type' => 'fieldset', 253 | '#title' => t('Privacy Settings'), 254 | '#description' => t('Define whether or not user information can be used'), 255 | ); 256 | 257 | // enable visitor recognition config 258 | $enable_visitor_recognition_attr = array(); 259 | if (!is_null($display_opts) && isset($display_opts->enable_visitor_recognition) && $display_opts->enable_visitor_recognition) { 260 | $enable_visitor_recognition_attr['checked'] = true; 261 | } 262 | $form['enable_visitor_recognition'] = array( 263 | '#type' => 'checkbox', 264 | '#title' => t('Enable visitor recognition'), 265 | '#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'), 266 | '#id' => t('enable_visitor_recognition'), 267 | '#attributes' => $enable_visitor_recognition_attr, 268 | ); 269 | 270 | return $form; 271 | } 272 | 273 | /** 274 | * tawk.to admin form submit handler 275 | */ 276 | function tawk_to_admin_form_submit($form, &$form_state) { 277 | $jsonOpts = array( 278 | 'always_display' => false, 279 | 'hide_oncustom' => array(), 280 | 'show_onfrontpage' => false, 281 | 'show_oncategory' => false, 282 | 'show_oncustom' => array(), 283 | 'enable_visitor_recognition' => false, 284 | ); 285 | 286 | $options = $form_state['values']; 287 | 288 | foreach ($options as $column => $value) { 289 | switch ($column) { 290 | case 'hide_oncustom': 291 | case 'show_oncustom': 292 | // split by newlines, then remove empty lines 293 | $value = str_replace("\r", "\n", $value); 294 | $value = explode("\n", $value); 295 | $non_empty_values = array(); 296 | foreach ($value as $str) { 297 | $trimmed = trim($str); 298 | if ($trimmed !== '') { 299 | $non_empty_values[] = $trimmed; 300 | } 301 | } 302 | $jsonOpts[$column] = json_encode($non_empty_values); 303 | break; 304 | case 'show_onfrontpage': 305 | case 'show_oncategory': 306 | case 'always_display': 307 | case 'enable_visitor_recognition': 308 | $jsonOpts[$column] = $value == 1; 309 | break; 310 | } 311 | } 312 | 313 | variable_set( 314 | TawkHelper::TAWK_TO_WIDGET_OPTIONS, 315 | json_encode($jsonOpts) 316 | ); 317 | 318 | // Flush page cache so widget options will update at next page load 319 | cache_clear_all(NULL, 'cache_page'); 320 | 321 | drupal_set_message('Successfully set widget options to your site'); 322 | } 323 | 324 | /** 325 | * Helper function to add new lines to url paths for displaying in textarea fields 326 | */ 327 | function format_multiline_url_paths($url_paths) { 328 | $paths = json_decode($url_paths); 329 | $formatted_paths = ''; 330 | foreach($paths as $path) { 331 | if (!empty($formatted_paths)) { 332 | $formatted_paths .= "\r\n"; 333 | } 334 | 335 | $formatted_paths .= $path; 336 | } 337 | return $formatted_paths; 338 | } 339 | --------------------------------------------------------------------------------