diff --git a/eu_cookie_compliance.admin.inc b/eu_cookie_compliance.admin.inc index 7b44f4a..8533f95 100755 --- a/eu_cookie_compliance.admin.inc +++ b/eu_cookie_compliance.admin.inc @@ -9,98 +9,110 @@ */ function eu_cookie_compliance_admin_form($form_state) { - global $language; - $ln = $language->language; - $popup_settings = eu_cookie_compliance_get_settings(); - $form['eu_cookie_compliance_' . $ln] = array ( - '#type' => 'item', - '#tree' => TRUE, - ); + $path = drupal_get_path('module', 'eu_cookie_compliance'); + drupal_add_js($path . '/js/eu_cookie_compliance.admin.js', 'module'); + global $language; if (module_exists('locale')) { - $form['eu_cookie_compliance_' . $ln]['#title'] = t('You are editing settings for the %language language.', array('%language' => $language->name)); + $languages = locale_language_list(); + } + else { + $languages = array($language->language => $language->native); } - $form['eu_cookie_compliance_' . $ln]['popup_enabled'] = array( - '#type' => 'checkbox', - '#title' => t('Enable popup'), - '#default_value' => ($popup_settings['popup_enabled']) ? $popup_settings['popup_enabled'] : 0, - ); - - $form['eu_cookie_compliance_' . $ln]['popup_position'] = array( - '#type' => 'checkbox', - '#title' => t('Place the pop-up at the top of the website'), - '#default_value' => ($popup_settings['popup_position']) ? $popup_settings['popup_position'] : 0, - '#description' => t('By default the pop-up appears at the bottom of the website. Tick this box if you want it to appear at the top'), - ); - - $form['eu_cookie_compliance_' . $ln]['popup_info'] = array( - '#type' => 'item', - '#tree' => TRUE, - ); - - $form['eu_cookie_compliance_' . $ln]['popup_info']['value'] = array( - '#type' => 'textarea', - '#title' => t('Popup message - requests consent'), - '#default_value' => ($popup_settings['popup_info']['value']) ? $popup_settings['popup_info']['value'] : '', - '#required' => TRUE, - ); - - $form['eu_cookie_compliance_' . $ln]['popup_info']['format'] = filter_form($popup_settings['popup_info']['format'], NULL, array('eu_cookie_compliance_' . $language->language, 'popup_info', 'format')); - - $form['eu_cookie_compliance_' . $ln]['popup_agreed'] = array( - '#type' => 'item', - '#tree' => TRUE, - ); - - $form['eu_cookie_compliance_' . $ln]['popup_agreed']['value'] = array( - '#type' => 'textarea', - '#title' => t('Popup message - thanks for giving consent'), - '#default_value' => ($popup_settings['popup_agreed']['value']) ? $popup_settings['popup_agreed']['value'] : '', - '#required' => TRUE, - ); - - $form['eu_cookie_compliance_' . $ln]['popup_agreed']['format'] = filter_form($popup_settings['popup_agreed']['format'], NULL, array('eu_cookie_compliance_' . $language->language, 'popup_agreed', 'format')); - - $form['eu_cookie_compliance_' . $ln]['popup_link'] = array( - '#type' => 'textfield', - '#title' => t('Private policy link'), - '#default_value' => ($popup_settings['popup_link']) ? $popup_settings['popup_link'] : '', - '#size' => 60, - '#maxlength' => 220, - '#required' => TRUE, - '#description' => t('Enter link to your privacy policy or other page that will explain cookies to your users. For external links prepend http://'), - ); - - $form['eu_cookie_compliance_' . $ln]['popup_height'] = array( - '#type' => 'textfield', - '#title' => t('Popup height in pixels'), - '#default_value' => ($popup_settings['popup_height']) ? $popup_settings['popup_height'] : '', - '#size' => 5, - '#maxlength' => 5, - '#required' => FALSE, - '#description' => t('Enter an integer value for a desired height in pixels or leave empty for automatically adjusted height'), - - ); - - $form['eu_cookie_compliance_' . $ln]['popup_width'] = array( - '#type' => 'textfield', - '#title' => t('Popup width in pixels or a percentage value'), - '#default_value' => ($popup_settings['popup_width']) ? $popup_settings['popup_width'] : '100%', - '#size' => 5, - '#maxlength' => 5, - '#required' => TRUE, - '#description' => t('Set the width of the popup. This can be either an integer value or percentage of the screen width. For example: 200 or 50%'), + $form['languages'] = array( + '#type' => 'hidden', + '#value' => $languages, ); - $form['eu_cookie_compliance_' . $ln]['popup_delay'] = array( - '#type' => 'textfield', - '#title' => t('Popup time delay in seconds'), - '#default_value' => ($popup_settings['popup_delay']) ? $popup_settings['popup_delay'] : 1, - '#size' => 5, - '#maxlength' => 5, - '#required' => TRUE, - ); + foreach ($languages as $ln => $lname) { + $popup_settings = eu_cookie_compliance_get_settings('all', $ln); + + $form['eu_cookie_compliance_' . $ln] = array( + '#type' => 'item', + '#tree' => TRUE, + ); + if (module_exists('locale')) { + $form['eu_cookie_compliance_' . $ln]['#title'] = t('You are editing settings for the %language language.', array('%language' => $lname)); + } + $form['eu_cookie_compliance_' . $ln]['popup_enabled'] = array( + '#type' => 'checkbox', + '#title' => t('Enable popup'), + '#prefix' => '
', + '#suffix' => '
', + '#default_value' => ($popup_settings['popup_enabled']) ? $popup_settings['popup_enabled'] : 0, + ); + $form['eu_cookie_compliance_' . $ln]['popup_position'] = array( + '#prefix' => '
', + '#type' => 'checkbox', + '#title' => t('Place the pop-up at the top of the website'), + '#default_value' => ($popup_settings['popup_position']) ? $popup_settings['popup_position'] : 0, + '#description' => t('By default the pop-up appears at the bottom of the website. Tick this box if you want it to appear at the top'), + ); + $form['eu_cookie_compliance_' . $ln]['popup_info'] = array( + '#type' => 'item', + '#tree' => TRUE, + ); + $form['eu_cookie_compliance_' . $ln]['popup_info']['value'] = array( + '#type' => 'textarea', + '#title' => t('Popup message - requests consent*'), + '#default_value' => ($popup_settings['popup_info']['value']) ? $popup_settings['popup_info']['value'] : '', + '#description' => t('Required'), + ); + $form['eu_cookie_compliance_' . $ln]['popup_info']['format'] = filter_form($popup_settings['popup_info']['format'], NULL, array( + 'eu_cookie_compliance_' . $ln, + 'popup_info', + 'format')); + $form['eu_cookie_compliance_' . $ln]['popup_agreed'] = array( + '#type' => 'item', + '#tree' => TRUE, + ); + $form['eu_cookie_compliance_' . $ln]['popup_agreed']['value'] = array( + '#type' => 'textarea', + '#title' => t('Popup message - thanks for giving consent*'), + '#default_value' => ($popup_settings['popup_agreed']['value']) ? $popup_settings['popup_agreed']['value'] : '', + '#description' => t('Required'), + ); + $form['eu_cookie_compliance_' . $ln]['popup_agreed']['format'] = filter_form($popup_settings['popup_agreed']['format'], NULL, array( + 'eu_cookie_compliance_' . $ln, + 'popup_agreed', + 'format')); + $form['eu_cookie_compliance_' . $ln]['popup_link'] = array( + '#type' => 'textfield', + '#title' => t('Private policy link*'), + '#default_value' => ($popup_settings['popup_link']) ? $popup_settings['popup_link'] : '', + '#size' => 60, + '#maxlength' => 220, + '#description' => t('Required. Enter link to your privacy policy or other page that will explain cookies to your users. For external links prepend http://'), + ); + $form['eu_cookie_compliance_' . $ln]['popup_height'] = array( + '#type' => 'textfield', + '#title' => t('Popup height in pixels'), + '#default_value' => ($popup_settings['popup_height']) ? $popup_settings['popup_height'] : '', + '#size' => 5, + '#maxlength' => 5, + '#required' => FALSE, + '#description' => t('Enter an integer value for a desired height in pixels or leave empty for automatically adjusted height'), + ); + $form['eu_cookie_compliance_' . $ln]['popup_width'] = array( + '#type' => 'textfield', + '#title' => t('Popup width in pixels or a percentage value*'), + '#default_value' => ($popup_settings['popup_width']) ? $popup_settings['popup_width'] : '100%', + '#size' => 5, + '#maxlength' => 5, + '#description' => t('Required. Set the width of the popup. This can be either an integer value or percentage of the screen width. For example: 200 or 50%'), + ); + $form['eu_cookie_compliance_' . $ln]['popup_delay'] = array( + '#type' => 'textfield', + '#title' => t('Popup time delay in seconds*'), + '#default_value' => ($popup_settings['popup_delay']) ? $popup_settings['popup_delay'] : 1, + '#size' => 5, + '#maxlength' => 5, + '#description' => t('Required'), + // Close div for this language. + '#suffix' => '
', + ); + } return system_settings_form($form); } @@ -109,16 +121,34 @@ function eu_cookie_compliance_admin_form($form_state) { * Validates form for cookie controll popup. */ function eu_cookie_compliance_admin_form_validate($form, &$form_state) { - global $language; - $ln = $language-> language; - //print_r($form_state); die; - if (!preg_match('/^[1-9][0-9]{0,4}$/', $form_state['values']['eu_cookie_compliance_' . $ln]['popup_height']) && !empty($form_state['values']['eu_cookie_compliance_' . $ln]['popup_height'])) { - form_set_error('eu_cookie_compliance_popup_height', t('Height must be an integer value .')); - } - if (!preg_match('/^[1-9][0-9]{0,4}$/', $form_state['values']['eu_cookie_compliance_' . $ln]['popup_delay'])) { - form_set_error('eu_cookie_compliance_popup_delay', t('Delay must be an integer value.')); - } - if (!preg_match('/^[1-9][0-9]{0,4}\%?$/', $form_state['values']['eu_cookie_compliance_' . $ln]['popup_width'])) { - form_set_error('eu_cookie_compliance_popup_width', t('Width must be an integer or a percentage value.')); + $languages = $form['languages']['#value']; + foreach ($languages as $ln => $lname) { + // Don't validate unless language is enabled. + if ($form_state['values']['eu_cookie_compliance_' . $ln]['popup_enabled']) { + if (!preg_match('/^[1-9][0-9]{0,4}$/', $form_state['values']['eu_cookie_compliance_' . $ln]['popup_height']) && !empty($form_state['values']['eu_cookie_compliance_' . $ln]['popup_height'])) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_height', t('Height must be an integer value.')); + } + if (!preg_match('/^[1-9][0-9]{0,4}$/', $form_state['values']['eu_cookie_compliance_' . $ln]['popup_delay'])) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_delay', t('Delay must be an integer value.')); + } + if (!preg_match('/^[1-9][0-9]{0,4}\%?$/', $form_state['values']['eu_cookie_compliance_' . $ln]['popup_width'])) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_width', t('Width must be an integer or a percentage value.')); + } + if (!$form_state['values']['eu_cookie_compliance_' . $ln]['popup_info']) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_info', t('Please include popup info.')); + } + if (!$form_state['values']['eu_cookie_compliance_' . $ln]['popup_agreed']) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_agreed', t('Please include popup agreed.')); + } + if (!$form_state['values']['eu_cookie_compliance_' . $ln]['popup_link']) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_link', t('Please include a popup link.')); + } + if (!$form_state['values']['eu_cookie_compliance_' . $ln]['popup_width']) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_width', t('Please include a popup width.')); + } + if (!$form_state['values']['eu_cookie_compliance_' . $ln]['popup_delay']) { + form_set_error('eu_cookie_compliance_' . $ln . '][popup_delay', t('Please include a popup delay.')); + } + } } } diff --git a/eu_cookie_compliance.module b/eu_cookie_compliance.module index 3a288fa..5b882e7 100755 --- a/eu_cookie_compliance.module +++ b/eu_cookie_compliance.module @@ -1,4 +1,5 @@ language, array()); - if ($setting == 'all') return $popup_settings; +function eu_cookie_compliance_get_settings($setting = 'all', $language_prefix = '') { + if (!$language_prefix) { + global $language; + $language_prefix = $language->language; + } + $popup_settings = variable_get('eu_cookie_compliance_' . $language_prefix, array()); + if ($setting == 'all') { + return $popup_settings; + } return $popup_settings[$setting]; -} \ No newline at end of file +} diff --git a/js/eu_cookie_compliance.admin.js b/js/eu_cookie_compliance.admin.js new file mode 100644 index 0000000..17b43dd --- /dev/null +++ b/js/eu_cookie_compliance.admin.js @@ -0,0 +1,13 @@ +Drupal.behaviors.eu_cookie_compliance_admin = function(context) { + $(".enable-popup input:not(:checked)").each(function() { + lang = $(this).attr("id").split('edit-eu-cookie-compliance-'); + lang = lang[1].split('-popup-enabled'); + $('#' + lang[0] + '-popup-settings').hide(); + }); + $(".enable-popup input").click(function() { + lang = $(this).attr("id").split('edit-eu-cookie-compliance-'); + lang = lang[1].split('-popup-enabled'); + $('#' + lang[0] + '-popup-settings').toggle(); + console.log(lang[0] + '-popup-settings'); + }); +} diff --git a/js/eu_cookie_compliance.js b/js/eu_cookie_compliance.js old mode 100755 new mode 100644 index 2e8be54..258d316 --- a/js/eu_cookie_compliance.js +++ b/js/eu_cookie_compliance.js @@ -56,18 +56,9 @@ Drupal.eu_cookie_compliance.attachEvents = function() { } Drupal.eu_cookie_compliance.getCurrentStatus = function() { - var search = 'cookie-agreed-'+Drupal.settings.eu_cookie_compliance.popup_language+'='; - var offset = document.cookie.indexOf(search); - if (offset < 1) { - return 0; - } - offset += search.length; - var end = document.cookie.indexOf(';', offset); - if (end == -1) { - end = document.cookie.length; - } - var value = document.cookie.substring(offset, end); - return parseInt(value); + name = 'cookie-agreed'; + value = Drupal.eu_cookie_compliance.getCookie(name); + return value; } Drupal.eu_cookie_compliance.changeStatus = function(value) { @@ -96,7 +87,7 @@ Drupal.eu_cookie_compliance.changeStatus = function(value) { } var date = new Date(); date.setDate(date.getDate() + 100); - document.cookie = "cookie-agreed-"+Drupal.settings.eu_cookie_compliance.popup_language + "="+value+";expires=" + date.toUTCString() + ";path=/"; + document.cookie = "cookie-agreed=" + value + ";expires=" + date.toUTCString() + ";path=/"; } @@ -107,3 +98,25 @@ Drupal.eu_cookie_compliance.hasAgreed = function() { } return false; } + +/** + * Verbatim copy of Drupal.comment.getCookie(). + */ +Drupal.eu_cookie_compliance.getCookie = function(name) { + var search = name + '='; + var returnValue = ''; + + if (document.cookie.length > 0) { + offset = document.cookie.indexOf(search); + if (offset != -1) { + offset += search.length; + var end = document.cookie.indexOf(';', offset); + if (end == -1) { + end = document.cookie.length; + } + returnValue = decodeURIComponent(document.cookie.substring(offset, end).replace(/\+/g, '%20')); + } + } + + return returnValue; +};