added: more options, better parameter handling, fancier settings page

This commit is contained in:
Niko Bochan 2015-02-13 15:56:42 +01:00
parent 72ef84fbda
commit 358ff46c98
4 changed files with 117 additions and 6 deletions

View File

@ -13,7 +13,10 @@ class CRM_Admin_Form_Setting_ProxySettings extends CRM_Admin_Form_Setting
// add all required elements // add all required elements
$this->addElement('checkbox','proxy_enabled'); $this->addElement('checkbox','proxy_enabled');
$this->addElement('text', 'proxy_url', ts('Proxy URL')); $this->addElement('text', 'proxy_url', ts('Proxy URL'), array('disabled' => 'disabled'));
$this->addElement('static', 'proxy_version', ts('Proxy version'));
$this->addElement('text', 'civimail_external_optout', ts('CiviMail: External out-out page'), array('disabled' => 'disabled'));
$this->addButtons(array( $this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE), array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE),
@ -25,12 +28,30 @@ class CRM_Admin_Form_Setting_ProxySettings extends CRM_Admin_Form_Setting
function addRules() { function addRules() {
$this->addRule('proxy_url', ts('This may only contain a valid URL'), 'onlyValidURL'); $this->addRule('proxy_url', ts('This may only contain a valid URL'), 'onlyValidURL');
$this->addRule('civimail_external_optout', ts('This may only contain a valid URL'), 'onlyValidURL');
} }
function preProcess() { function preProcess() {
$this->assign('proxy_enabled', CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_enabled')); $this->assign('proxy_enabled', CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_enabled'));
$proxyUrl = CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_url');
$proxyVersion = "-";
if($proxyUrl) {
// try to get the current proxy version
$response = $this->requestProxyVersion($proxyUrl);
if ($response['is_error']) {
$proxyVersion = $response['message'];
CRM_Core_BAO_Setting::setItem(NULL,'CiviProxy Settings', 'proxy_version');
}else{
$proxyVersion = $response['version'];
CRM_Core_BAO_Setting::setItem($proxyVersion,'CiviProxy Settings', 'proxy_version');
}
}
$this->setDefaults(array( $this->setDefaults(array(
'proxy_url' => CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'proxy_url'), 'proxy_url' => $proxyUrl,
'proxy_version' => $proxyVersion, // watch out, this might contain an error message
'civimail_external_optout' => CRM_Core_BAO_Setting::getItem('CiviProxy Settings', 'civimail_extoptout')
)); ));
} }
@ -42,9 +63,12 @@ class CRM_Admin_Form_Setting_ProxySettings extends CRM_Admin_Form_Setting
CRM_Core_BAO_Setting::setItem(!empty($values['proxy_enabled']),'CiviProxy Settings', 'proxy_enabled'); CRM_Core_BAO_Setting::setItem(!empty($values['proxy_enabled']),'CiviProxy Settings', 'proxy_enabled');
// text // text
if ($values['proxy_url']){ if (isset($values['proxy_url'])) {
CRM_Core_BAO_Setting::setItem($values['proxy_url'],'CiviProxy Settings', 'proxy_url'); CRM_Core_BAO_Setting::setItem($values['proxy_url'],'CiviProxy Settings', 'proxy_url');
} }
if (isset($values['civimail_external_optout'])) {
CRM_Core_BAO_Setting::setItem($values['civimail_external_optout'],'CiviProxy Settings', 'civimail_extoptout');
}
// give feedback to user // give feedback to user
$session = CRM_Core_Session::singleton(); $session = CRM_Core_Session::singleton();
@ -56,4 +80,25 @@ class CRM_Admin_Form_Setting_ProxySettings extends CRM_Admin_Form_Setting
return preg_match("/^(http(s?):\/\/)?(((www\.)?+[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,6})+)|(localhost)|(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b))(:[0-9]{1,5})?(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/",$value); return preg_match("/^(http(s?):\/\/)?(((www\.)?+[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,6})+)|(localhost)|(\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b))(:[0-9]{1,5})?(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/",$value);
} }
/* Performs an http request to the specified url and tries
* to parse the response in order to get the current proxy
* version
*
* @param $url url of the proxy to use
* @return array(int is_error, [string message || string version])
*/
function requestProxyVersion($url) {
$response = @file_get_contents($url);
if($response === FALSE) {
return array('is_error' => 1, 'message' => sprintf(ts('Error: cannot access "%s"'), $url));
}else{
$result = preg_match("/<p id=\"version\">CiviProxy Version ([0-9]+\.[0-9]+|[0-9]+\.[0-9]+\.[0-9]+)<\/p>/", $response, $output_array);
if ($result === FALSE || $result === 0){
return array('is_error' => 1, 'message' => sprintf(ts('Error: failed to parse version information'), $url));
}else{
return array('is_error' => 0, 'version' => $output_array[1]);
}
}
}
} }

View File

@ -30,4 +30,16 @@ return array(
'description' => 'The URL from which the proxy will be available for requests', 'description' => 'The URL from which the proxy will be available for requests',
'help_text' => 'TODO', 'help_text' => 'TODO',
), ),
'proxy_version' => array(
'group_name' => 'CiviProxy Settings',
'group' => 'de.systopia',
'name' => 'proxy_version',
'type' => 'String',
'default' => "",
'add' => '4.3',
'is_domain' => 1,
'is_contact' => 0,
'description' => 'The version of the currently selected proxy',
'help_text' => 'TODO',
),
); );

View File

@ -10,3 +10,15 @@
{ts}Enter your CiviProxy's URL here{/ts} {ts}Enter your CiviProxy's URL here{/ts}
</p> </p>
{/htxt} {/htxt}
{htxt id='id-proxy-version'}
<p>
{ts}TODO #1{/ts}
</p>
{/htxt}
{htxt id='id-extoptout-url'}
<p>
{ts}TODO #2{/ts}
</p>
{/htxt}

View File

@ -3,7 +3,7 @@
<h3>{ts}Core Settings{/ts}</h3> <h3>{ts}Core Settings{/ts}</h3>
<div> <div>
<div> <div>
<table id="core_settings"> <table id="core_settings" class="no-border">
<tr> <tr>
<td class="label"><label for="proxy_enabled"> {ts}Enable proxy{/ts} <a onclick='CRM.help("{ts}Enable Proxy{/ts}", {literal}{"id":"id-proxy-enabled","file":"CRM\/Admin\/Form\/Setting\/ProxySettings"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon">&nbsp;</a></label></td> <td class="label"><label for="proxy_enabled"> {ts}Enable proxy{/ts} <a onclick='CRM.help("{ts}Enable Proxy{/ts}", {literal}{"id":"id-proxy-enabled","file":"CRM\/Admin\/Form\/Setting\/ProxySettings"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon">&nbsp;</a></label></td>
<td><input value="1" type="checkbox" id="proxy_enabled" name="proxy_enabled" {if $proxy_enabled}checked="checked"{/if} class="form-checkbox"/></td> <td><input value="1" type="checkbox" id="proxy_enabled" name="proxy_enabled" {if $proxy_enabled}checked="checked"{/if} class="form-checkbox"/></td>
@ -12,6 +12,23 @@
<td class="label">{$form.proxy_url.label} <a onclick='CRM.help("{ts}Proxy URL{/ts}", {literal}{"id":"id-proxy-url","file":"CRM\/Admin\/Form\/Setting\/ProxySettings"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon">&nbsp;</a></td> <td class="label">{$form.proxy_url.label} <a onclick='CRM.help("{ts}Proxy URL{/ts}", {literal}{"id":"id-proxy-url","file":"CRM\/Admin\/Form\/Setting\/ProxySettings"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon">&nbsp;</a></td>
<td>{$form.proxy_url.html}</td> <td>{$form.proxy_url.html}</td>
</tr> </tr>
<tr>
<td class="label">{$form.proxy_version.label} <a onclick='CRM.help("{ts}Proxy Version{/ts}", {literal}{"id":"id-proxy-version","file":"CRM\/Admin\/Form\/Setting\/ProxySettings"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon">&nbsp;</a></td>
<td>{$form.proxy_version.html}</td>
</tr>
</table>
</div>
</div>
</div>
<div>
<h3>{ts}Component Settings{/ts}</h3>
<div>
<div>
<table id="component_settings" class="no-border">
<tr>
<td class="label">{$form.civimail_external_optout.label} <a onclick='CRM.help("{ts}CiviMail: External out-out page{/ts}", {literal}{"id":"id-extoptout-url","file":"CRM\/Admin\/Form\/Setting\/ProxySettings"}{/literal}); return false;' href="#" title="{ts}Help{/ts}" class="helpicon">&nbsp;</a></td>
<td>{$form.civimail_external_optout.html}</td>
</tr>
</table> </table>
</div> </div>
</div> </div>
@ -21,14 +38,39 @@
{literal} {literal}
<script type="text/javascript"> <script type="text/javascript">
function enableInput() {
cj('#proxy_url').attr('disabled',!this.checked);
cj('#civimail_external_optout').attr('disabled', !this.checked);
}
function enableInputGlobal() {
var is_enabled = cj('#proxy_enabled').attr('checked') == 'checked';
cj('#proxy_url').attr('disabled', !is_enabled);
cj('#civimail_external_optout').attr('disabled', !is_enabled);
}
(function(cj) { (function(cj) {
cj('#proxy_enabled').click(enableInput);
enableInputGlobal();
})(cj); })(cj);
</script> </script>
<style type="text/css"> <style type="text/css">
#proxy_url { #proxy_url, #civimail_external_optout {
width: 300px; width: 350px;
}
.no-border,
.no-border tr,
.no-border tbody td,
.no-border thead th,
.no-border tfoot th {
border: none;
}
.label {
min-width: 200px;
} }
</style> </style>
{/literal} {/literal}