forked from marcostoll/processwire-fieldtype-assisted-url
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFieldtypeAssistedURL.module
109 lines (100 loc) · 3.17 KB
/
FieldtypeAssistedURL.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Class FieldtypeAssistedURL
*
* Modified by Adrian Jones to support storing URL as page ID
*
* @author Marco Stoll <[email protected]>
* @link http://core4.de CORE4 GmbH & Co. KG
* @copyright Copyright (c) 2015, CORE4 GmbH & Co. KG
* @license MIT http://opensource.org/licenses/MIT
* @version 1.0.0
* @see http://www.processwire.com
* @filesource
*/
/**
* Class FieldtypeAssistedURL
*/
class FieldtypeAssistedURL extends FieldtypeURL {
/**
* Get information about this module
*
* @return array
*/
public static function getModuleInfo() {
return [
'title' => 'AssistedURL Field',
'version' => '2.0.1',
'summary' => 'Field that stores an URL providing controls for selecting internal pages and files.',
'author' => 'Marco Stoll, Adrian Jones',
'installs' => 'InputfieldAssistedURL',
];
}
/**
* Per Module interface, this template method is called when all system classes are loaded and ready for API usage
*/
public function init() {
parent::init();
}
/**
* Return new instance of the Inputfield associated with this Fieldtype
*
* @param Page $page
* @param Field $field
* @return Inputfield
*/
public function getInputfield(Page $page, Field $field) {
/** @var InputfieldAssistedURL $inputField */
$inputField = $this->modules->get("InputfieldAssistedURL");
$inputField->setPage($page);
return $inputField;
}
/**
* get values converted when fetched from db
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
$urlParts = explode("?", $value);
if(is_numeric($urlParts[0])) {
$return = $this->wire('pages')->get($urlParts[0])->url . (isset($urlParts[1]) ? '?' . $urlParts[1] : '');
}
else {
$return = $value;
}
return $return;
}
/**
* return converted from object to array for storing in database
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
if(!$value) return '';
foreach($this->wire('config')->httpHosts as $host) {
$withoutHttp = preg_replace('#^https?://#', '', $value);
if (strpos($withoutHttp, $host) === 0) {
$value = str_replace($host, '', $value);
$value = preg_replace('#^https?://#', '', $value);
}
}
if(strpos($value, '//') === false && strpos($value, 'mailto:') === false) {
$urlParts = explode("?", $value);
if($this->wire('config')->urls->root !== '/') {
$urlPage = "/" . str_replace($this->wire('config')->urls->root, "", $urlParts[0]);
}
else {
$urlPage = $urlParts[0];
}
$p = $this->wire('pages')->get(trim($urlPage, '.'));
if($p->id) {
$return = $p->id . (isset($urlParts[1]) ? '?' . $urlParts[1] : '');
}
else {
$return = $value;
}
}
else {
$return = $value;
}
return $return;
}
}