You are here

public function TawktoGenerator::setOptions in tawk.to Live Chat (Drupal 8) 8

File

tawk_to/src/core/TawktoGenerator.php, line 382

Class

TawktoGenerator

Namespace

Drupal\tawk_to\core

Code

public function setOptions($options) {
  if (!$options) {
    return new JsonResponse(array(
      'success' => false,
    ));
  }
  $jsonOpts = array(
    'always_display' => false,
    'hide_oncustom' => false,
    'show_onfrontpage' => false,
    'show_oncategory' => false,
    'show_oncustom' => array(),
  );
  $options = explode('&', $options);
  foreach ($options as $post) {
    list($column, $value) = explode('=', $post);
    switch ($column) {
      case 'hide_oncustom':
      case 'show_oncustom':

        // replace newlines and returns with comma, and convert to array for saving
        $value = urldecode($value);
        $value = str_ireplace([
          "\r\n",
          "\r",
          "\n",
        ], ',', $value);
        $value = explode(",", $value);
        $value = empty($value) || !$value ? array() : $value;
        $jsonOpts[$column] = json_encode($value);
        break;
      case 'show_onfrontpage':
      case 'show_oncategory':
      case 'always_display':

        // default:
        $jsonOpts[$column] = $value == 1 ? true : false;
        break;
    }
  }
  $config = \Drupal::service('config.factory')
    ->getEditable('tawk_to.settings');
  $config
    ->set('tawk_to.options', json_encode($jsonOpts));
  $config
    ->save();
  return new JsonResponse(array(
    'success' => true,
  ));
}