forked from Da-Fecto/FieldtypeSelectFile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypeSelectFile.module
More file actions
200 lines (171 loc) · 6.42 KB
/
FieldtypeSelectFile.module
File metadata and controls
200 lines (171 loc) · 6.42 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Fieldtype 'select file' stores a file/folder name selected in the associated
* Inputfield.
*
* ©2015 Martijn Geerts
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class FieldtypeSelectFile extends FieldtypeText {
/**
* Return an array of module information
*
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => __('Fieldtype select file'),
'version' => 104,
'summary' => __('Fieldtype that stores a file or folder.'),
'author' => 'Martijn Geerts',
'href' => 'https://processwire.com/talk/topic/6377-fieldtypeselectfile-inputfieldselectfile/',
'installs' => 'InputfieldSelectFile',
);
}
/**
* This method is called when all system classes are loaded and ready for API usage
*
*/
public function init() {
parent::init();
$this->allowTextFormatters(false);
$this->addHookBefore('Page::loaded', $this, 'changePageTemplate');
}
/**
* Change the page template to render
*
*/
public function changePageTemplate(HookEvent $event) {
// Page before loaded
$page = $event->object;
// Inputfield object
$inputfield = $page->fields->get('type=FieldtypeSelectFile');
// Is not of type FieldtypeSelectFile
if ($inputfield === NULL) return;
// If change page template is not set in the Inputfield config
if (!$inputfield->template) return;
$value = trim($page->get($inputfield->name));
// If no value return
if (!$value) return;
$path = $this->config->paths->templates;
$folder = ltrim($inputfield->folderPath, '/') . '/';
$filename = $path . $folder . $value;
if (!is_file($filename)) return;
$page->template->set('filename', $filename);
}
/**
* Sanitize value for storage
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
$file = $this->config->paths->templates . trim(trim($field->folderPath, '/')) . '/' . $value;
if(is_file($file) || is_dir($file)) return $value;
return '';
}
/**
* 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) {
$inputfield = $this->modules->get('InputfieldSelectFile');
$inputfield->set('folderPath', $field->folderPath);
$inputfield->set('fileExt', $field->fileExt);
$inputfield->set('hideFiles', $field->hideFiles);
$inputfield->set('hideFolders', $field->hideFolders);
$inputfield->set('sort', $field->sort);
$inputfield->set('template', $field->template);
return $inputfield;
}
/**
* Get the inputfield used for configuration of this Fieldtype.
*
* @param Field $field
* @return InputfieldWrapper
*
*/
public function ___getConfigInputfields(Field $field) {
$error = false;
if ($field->folderPath) {
$folder = $this->config->paths->templates . ltrim($field->folderPath, '/');
if (!is_dir($folder)) {
$path = $this->config->urls->templates . ltrim($field->folderPath, '/');
$error = sprintf($this->_("Folder %s doesn't exist."), $path);
}
}
$inputfields = parent::___getConfigInputfields($field);
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'folderPath');
$f->label = $this->_("The folder containing the files and/or folders.");
$f->attr('value', $field->folderPath);
$f->description =
sprintf($this->_('A relative path relative to the **%s** folder.'), $this->config->urls->templates) .
' ' .
sprintf($this->_('Leave blank for the **%s** folder.'), $this->config->urls->templates);
$f->notes =
$this->_("When the files are located in /site/templates/scripts/, type: scripts/");
$f->getErrors(true);
if ($error) $f->error($error);
$inputfields->add($f);
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'fileExt');
$f->label = $this->_("Hide File Extension");
$f->attr('autocheck', 1);
$f->attr('uncheckedValue', 0);
$f->attr('checkedValue', 1);
$f->columnWidth = 25;
$f->attr('value', $field->fileExt);
$inputfields->add($f);
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'hideFiles');
$f->label = $this->_("Hide Files");
$f->attr('autocheck', 1);
$f->attr('uncheckedValue', 0);
$f->attr('checkedValue', 1);
$f->columnWidth = 25;
$f->attr('value', $field->hideFiles);
$inputfields->add($f);
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'hideFolders');
$f->label = $this->_("Hide Folders");
$f->attr('autocheck', 1);
$f->attr('uncheckedValue', 0);
$f->attr('checkedValue', 1);
$f->columnWidth = 25;
$f->attr('value', $field->hideFolders);
$inputfields->add($f);
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'sort');
$f->label = $this->_("Natural Sort (Select options)");
$f->attr('autocheck', 1);
$f->attr('uncheckedValue', 0);
$f->attr('checkedValue', 1);
$f->columnWidth = 25;
$f->attr('value', $field->sort);
$inputfields->add($f);
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'template');
$f->label = $this->_('Change Page Template');
$f->label2 = $this->_('Use selected file as template');
$f->description =
$this->_('Just before the **Page::loaded** event the selected file is set as template file for the page.') .
' ' .
$this->_('This setting can only be applied once per a page and folders are exluded from the select inputfield.');
$f->attr('autocheck', 1);
$f->attr('uncheckedValue', 0);
$f->attr('checkedValue', 1);
$f->attr('value', $field->template);
$inputfields->add($f);
return $inputfields;
}
}