<?php
/**
 * @file
 * This module intends to deal with the EU Directive on Privacy and Electronic
 * Communications that comes into effect in the UK on 26th May 2012.
 *
 * Author: Marcin Pajdzik
 */

/**
 * Implements hook_menu().
 */
function eu_cookie_compliance_menu() {
  $items['admin/settings/eu-cookie-compliance'] = array(
    'title' => 'EU Cookie Compliance',
    'description' => 'Make your website compliant with the EU Directive on Privacy and Electronic Communications.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('eu_cookie_compliance_admin_form'),
    'access arguments' => array('administer EU Cookie Compliance popup'),
    'file' => 'eu_cookie_compliance.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_init().
 */
function eu_cookie_compliance_init() {
  $enabled = eu_cookie_compliance_get_settings('popup_enabled');
  if ($enabled && user_access( 'see EU Cookie Compliance popup' ) ) {
    $path = drupal_get_path('module', 'eu_cookie_compliance');
    drupal_add_css($path . '/css/eu_cookie_compliance.css');
    drupal_add_js($path . '/js/eu_cookie_compliance.js', 'module', 'footer');
  }
}

/**
 * Implements hook_footer().
 */

function eu_cookie_compliance_footer() {
  $popup_settings = eu_cookie_compliance_get_settings();
  if (!empty($popup_settings['popup_enabled']) && user_access( 'see EU Cookie Compliance popup' )) {
    global $language;
    $popup_text_info = str_replace(array("\r", "\n"), '', $popup_settings['popup_info']['value']);
    $popup_text_agreed = str_replace(array("\r", "\n"), '', $popup_settings['popup_agreed']['value']);
    $html_info = theme('eu_cookie_compliance_popup_info', check_markup($popup_text_info, $popup_settings['popup_info']['format'], FALSE),$popup_settings['popup_agree_button_message'],$popup_settings['popup_disagree_button_message']);
    $html_agreed = theme('eu_cookie_compliance_popup_agreed', check_markup($popup_text_agreed, $popup_settings['popup_agreed']['format'], FALSE),$popup_settings['popup_hide_button_message'],$popup_settings['popup_find_more_button_message']);
    $clicking_confirmation = (isset($popup_settings['popup_clicking_confirmation']))? $popup_settings['popup_clicking_confirmation'] : TRUE ;
    $variables = array(
      'popup_enabled' => $popup_settings['popup_enabled'],
      'custom_cookie_enabled' => $popup_settings['custom_cookie_domain_enable'],
      'custom_cookie_domain' => $popup_settings['custom_cookie_domain'],
      'popup_agreed_enabled' => $popup_settings['popup_agreed_enabled'],
      'popup_hide_agreed' => isset($popup_settings['popup_hide_agreed']) ? $popup_settings['popup_hide_agreed'] : FALSE,
      'popup_clicking_confirmation' => $clicking_confirmation,
      'popup_html_info' => empty($html_info) ? FALSE : $html_info,
      'popup_html_agreed' => empty($html_agreed) ? FALSE : $html_agreed,
      'popup_height' => ($popup_settings['popup_height']) ? (int) $popup_settings['popup_height'] : 'auto',
      'popup_width' => (drupal_substr($popup_settings['popup_width'], -1) == '%') ? $popup_settings['popup_width'] : (int) $popup_settings['popup_width'],
      'popup_delay' => (int) ($popup_settings['popup_delay'] * 1000),
      'popup_link' => empty($popup_settings['popup_link']) ? FALSE : $popup_settings['popup_link'],
      'popup_position' => empty($popup_settings['popup_position']) ? NULL : $popup_settings['popup_position'],
      'popup_language' => $language->language,
    );
    drupal_add_js(array('eu_cookie_compliance' => $variables), 'setting', 'footer');
  }
}

/**
 * Implements hook_perm().
 */
function eu_cookie_compliance_perm() {
  return array(
    'administer EU Cookie Compliance popup',
    'see EU Cookie Compliance popup',
  );
}

/**
 * Implements hook_theme().
 */
function eu_cookie_compliance_theme() {
  $path = drupal_get_path('module', 'eu_cookie_compliance') . '/theme';
  return array(
    'eu_cookie_compliance_popup_info' => array(
      'template' => 'eu-cookie-compliance-popup-info',
      'arguments' => array('message' => NULL, 'agree_button' => NULL, 'disagree_button' => NULL),
      'path' => $path,
    ),
    'eu_cookie_compliance_popup_agreed' => array(
      'template' => 'eu-cookie-compliance-popup-agreed',
      'arguments' => array('message' => NULL, 'hide_button' => NULL, 'find_more_button' => NULL),
      'path' => $path,
    ),
  );
}

/**
 *
 * Retrieves settings from the database for a current language.
 *
 * @global object $language
 *
 * @param object $setting
 *   A string indicating which setting to return (optional).
 *
 * @return array|string|NULL
 *   All settings are returned as an array if the function is called without
 *   arguments, or with the argument 'all'. If an argument is passed, a matching
 *   setting from the array stored in the variable is returned, otherwise NULL.
 */

function eu_cookie_compliance_get_settings($setting = 'all') {
  global $language;
  $popup_settings = variable_get('eu_cookie_compliance_' . $language->language, array());

  if ($setting == 'all') {
    return $popup_settings;
  }

  if (isset($popup_settings[$setting])) {
    return $popup_settings[$setting];
  } else {
    return NULL;
  }
}
