-
Notifications
You must be signed in to change notification settings - Fork 0
/
WireUtility.module
110 lines (90 loc) · 3.6 KB
/
WireUtility.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
110
<?php
class WireUtility extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'WireUtility',
'version' => 001,
'summary' => 'Utility functions for processwire',
'singular' => true,
'autoload' => true,
'permission' => "",
);
}
public function init() {
$class = $this->className();
define('WIRE_UTILITY_PATH', $this->config->paths->$class);
spl_autoload_register('WireUtilityLoader');
$this->config->inlineScripts = new InlineScriptArray();
}
/**
* Process the input from a submitted Page Edit form, delegating to other methods where appropriate
*
*/
public function ___processInput(Page $page, Inputfield $form, $level = 0) {
static $skipFields = array(
'sortfield_reverse',
'submit_publish',
'submit_save',
);
if(!$level) $form->processInput($this->input->post);
foreach($form as $inputfield) {
$name = $inputfield->attr('name');
if(in_array($name, $skipFields)) continue;
//if(!$page->editable($name)) { continue; }
//if($inputfield instanceof InputfieldFile) continue;
if($name == 'sortfield' && $this->useChildren) {
$this->processInputSortfield($inputfield) ;
continue;
}
if($this->useSettings) {
if($name == 'template') {
$this->processInputTemplate($inputfield);
continue;
}
if($name == 'status' && $this->processInputStatus($inputfield)) continue;
}
$page->set($name, $inputfield->value);
if($inputfield instanceof InputfieldWrapper && count($inputfield->getChildren())) $this->processInput($page, $inputfield, $level + 1);
}
}
public function ___require($form, $fields) {
foreach($fields as $fieldName) {
$field = $form->get($fieldName);
if(!$field) continue;
$value = $field->attr('value');
if(
($value instanceof PageArray && !$value->count()) ||
empty($value)
)
$field->error(_("{$field->label} is required"));
}
}
public function createInputFields(array $fieldConfig, $data) {
//Initialize fields
$inputfields = new InputfieldWrapper();
foreach($fieldConfig as $name => $config ) {
if(!isset($data[$name])) $data[$name] = isset($config['defaultValue']) ? $config['defaultValue'] : '';
$type = isset($config['type']) ? $config['type'] : 'InputfieldText';
$f = wire('modules')->get($type);
$f->attr('id+name', $name);
$f->label = $config['label'];
$f->description = isset($config['description']) ? $config['description'] : '';
//skip populating password field, unless we have post data
if($name !== 'password')
$f->value = $data[$name];
if(isset($config['attributes']))
foreach($config['attributes'] as $attr => $value) $f->attr($attr, $value);
$inputfields->append($f);
}
$password = wire('input')->post->password;
if(empty($password))
wire('input')->post->password=$data['password'];
return $inputfields;
}
}
function WireUtilityLoader($className) {
$file = WIRE_UTILITY_PATH . "include/$className.php";
if(is_file($file)) {
require($file);
}
}