praiadeseselle/wire/modules/Inputfield/InputfieldURL.module

121 lines
3.3 KiB
Text
Raw Permalink Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
2022-11-05 18:32:48 +01:00
* URL Inputfield Module
2022-03-08 15:55:41 +01:00
*
2022-11-05 18:32:48 +01:00
* An Inputfield for handling input of URLs in ProcessWire forms.
*
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
* https://processwire.com
2022-03-08 15:55:41 +01:00
*
* @property int|bool $noRelative Whether relative URLs are disabled
* @property int|bool $addRoot Whether to prepend root path
* @property int|bool $allowIDN Whether to allow IDNs
* @property int|bool $allowQuotes Whether to allow quote characters in URLs
*
*/
class InputfieldURL extends InputfieldText {
public static function getModuleInfo() {
return array(
'title' => __('URL', __FILE__), // Module Title
'summary' => __('URL in valid format', __FILE__), // Module Summary
2022-11-05 18:32:48 +01:00
'version' => 103,
);
2022-03-08 15:55:41 +01:00
}
2022-11-05 18:32:48 +01:00
/**
* Construct
*
*/
2022-03-08 15:55:41 +01:00
public function __construct() {
parent::__construct();
$this->setAttribute('type', 'text');
$this->setAttribute('maxlength', 1024);
$this->setAttribute('size', 0);
$this->setAttribute('name', 'href');
$this->label = 'URL';
$this->set('noRelative', 0); // whether relative URLs are disabled
$this->set('addRoot', 0); // whether to prepend root path
$this->set('allowIDN', 0); // whether to allow IDNs
$this->set('allowQuotes', 0); // whether to allow quote characters in URLs
}
2022-11-05 18:32:48 +01:00
/**
* Render Inputfield
*
* @return string
*
*/
2022-03-08 15:55:41 +01:00
public function ___render() {
2022-11-05 18:32:48 +01:00
$rootUrl = $this->wire()->config->urls->root;
2022-03-08 15:55:41 +01:00
if($this->addRoot && !$this->noRelative && !$this->notes && strlen($rootUrl) > 1) {
2022-11-05 18:32:48 +01:00
$this->notes = sprintf(
$this->_("Start local URLs with “/” and leave off the “%s” part."), // Instruction for local URLs displayed when site is running from a subdirectory
$rootUrl
);
2022-03-08 15:55:41 +01:00
}
return parent::___render();
}
2022-11-05 18:32:48 +01:00
/**
* Set value attribute
*
* @param string $value
* @return string
*
*/
2022-03-08 15:55:41 +01:00
protected function setAttributeValue($value) {
2022-11-05 18:32:48 +01:00
$value = trim((string) $value);
$dirty = $value;
$label = $this->label ? $this->label : $this->name;
if(!strlen($dirty)) return '';
$value = $this->wire()->sanitizer->url($dirty, array(
'allowRelative' => $this->noRelative ? false : true,
'allowIDN' => $this->allowIDN ? true : false,
'stripQuotes' => $this->allowQuotes ? false : true,
));
if(empty($value) || ($dirty !== $value && "http://$dirty" !== $value && "https://$dirty" !== $value)) {
$error = true;
if(strpos($dirty, '%') !== false) {
$test = rawurldecode($dirty);
if($value === $test || $value === "http://$test" || $value === "https://$test") $error = false;
}
if($error) {
$value = $this->val();
2022-11-05 18:32:48 +01:00
$this->error("$label - " . $this->_("Error found - please check that it is a valid URL")); // Error message when invalid URL found
2022-03-08 15:55:41 +01:00
}
2022-11-05 18:32:48 +01:00
} else if($value !== $dirty) {
$scheme = '';
foreach(array('http', 'https') as $s) {
if($value !== "$s://$dirty") continue;
$scheme = "$s://";
break;
}
if($scheme) {
$this->message("$label - " . sprintf($this->_("Note that %s was added"), $scheme)); // Message displayed when scheme was automatically added to the URL
}
}
2022-03-08 15:55:41 +01:00
return $value;
}
2022-11-05 18:32:48 +01:00
/**
* Configure Inputfield
*
* @return InputfieldWrapper
*
*/
2022-03-08 15:55:41 +01:00
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$f = $inputfields->get('stripTags');
if($f) $inputfields->remove($f);
return $inputfields;
}
}