<?php
// $Id: $

define(LOOPFUSE_NET_HOSTNAME, 'www.loopfuse.net');

/**
 * @file
 * Administrative page callbacks for the loopfusestatistics module.
 */

function loopfuse_menu() {
	$items[] = array(
		'title' => t('LoopFuse Integration'),
		'path' => 'admin/settings/loopfuse',
		'description' => t('Configure support for LoopFuse integration'),
		'callback' => 'drupal_get_form',
		'callback arguments' => array('loopfuse_admin_settings'),
		'access' => user_access('administer loopfuse integration'),
		'type' => MENU_NORMAL_ITEM
	);
	
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function loopfuse_perm() {
	return array('administer loopfuse integration');
}


/**
 * Implementation of hook_admin_settings() for configuring the module
 */
function loopfuse_admin_settings() {
  $form['account'] = array(
    '#type' => 'fieldset',
    '#title' => t('General settings'),
    '#collapsible' => FALSE,
  );

  $form['account']['loopfuse_account'] = array(
    '#type' => 'textfield',
    '#title' => t('LoopFuse account number'),
    '#default_value' => variable_get('loopfuse_account', 'LF_'),
    '#size' => 15,
    '#maxlength' => 20,
    '#required' => TRUE,
    '#description' => t(' Choose <strong>Administration -> CRM -> CRM Configuration</strong> menu item in your LoopFuse account, then select <strong>Website Integration</strong> at the right-hand side of the sub-menu to find the account number (LF_XXXXXXXX). You can obtain a user account from the <a href="@url">loopfuse</a> website.', array('@url' => 'http://loopfuse.net/')),
  );

  $form['account']['loopfuse_hostname'] = array(
    '#type' => 'textfield',
    '#title' => t('LoopFuse URL'),
    '#default_value' => variable_get('loopfuse_hostname', LOOPFUSE_NET_HOSTNAME),
    '#size' => 30,
    '#maxlength' => 100,
    '#description' => t('Base URL for your LoopFuse installation.  Use @name if you are using the hosted version of LoopFuse', array('@name' => LOOPFUSE_NET_HOSTNAME)),
  );

  $form['account']['loopfuse_protocol'] = array(
    '#type' => 'radios',
		'#options' => array('http' => 'HTTP', 'https' => 'HTTPS'),
    '#title' => t('LoopFuse URL'),
    '#default_value' => variable_get('loopfuse_protocol', 'http'),
    '#description' => t('The protocol used to access LoopFuse'),
  );


  return system_settings_form($form);
}

function loopfuse_admin_settings_validate($form, &$form_state) {
  if (!preg_match('/^LF_[[:xdigit:]]{8,}$/', trim($form_state['loopfuse_account']))) {
    form_set_error('loopfuse_account', t('A valid LoopFuse account number is case sensitive and formatted like LF_XXXXXXXX.'));
  }

  // Trim some text area values.
  $form_state['values']['loopfusestatistics_pages'] = trim($form_state['values']['loopfusestatistics_pages']);
}

function loopfuse_get_base_url() {
	return variable_get('loopfuse_protocol', 'http') . '://' . variable_get('loopfuse_hostname', LOOPFUSE_NET_HOSTNAME);
}

function loopfuse_get_hostname() {
	return variable_get('loopfuse_hostname', LOOPFUSE_NET_HOSTNAME);
}

function loopfuse_get_account() {
	return variable_get('loopfuse_account', NULL);
}