-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFieldtypeTemplates.module
More file actions
172 lines (134 loc) · 4.69 KB
/
FieldtypeTemplates.module
File metadata and controls
172 lines (134 loc) · 4.69 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
<?php
/**
* ProcessWire Templates (plural) Fieldtype
* By Hani AbuGhazaleh
* Based off of FieldtypeModules by Ryan Cramer
*
* Field that stores reference to one or more templates.
* Uses asmSelect to make the selection sortable/searchable.
*
* ProcessWire 2.x
* Copyright (C) 2012 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class FieldtypeTemplates extends FieldtypeMulti {
public static function getModuleInfo() {
return array(
'title' => 'Templates Reference (multiple)',
'version' => 100,
'summary' => 'Field that stores a reference to one or more templates and makes them sortable.',
'permanent' => false,
);
}
public function getBlankValue(Page $page, Field $field) {
return array();
}
public function sanitizeValue(Page $page, Field $field, $value) {
if (!$value)
return array();
if (!is_array($value))
$value = array($value);
foreach ($value as $k => $v) {
// we allow integers
if (is_int($v) && $this->templates->get($v))
continue;
// we allow templates
if ($v instanceof Template)
continue;
// but we don't allow anything else
unset($value[$k]);
}
return $value;
}
public function ___wakeupValue(Page $page, Field $field, $value) {
$v = $this->getBlankValue($page, $field);
if (empty($value))
return $v;
foreach ($value as $templateID) {
$v[] = (int) $templateID;
}
return $v;
}
public function ___sleepValue(Page $page, Field $field, $value) {
$a = array();
foreach ($value as $template) {
if (is_int($template))
$templateID = $template;
else
$templateID = $this->fuel('templates')->get($template)->id;
$a[] = $templateID;
}
return $a;
}
/**
* Instantiate the templates
*
* This method is only used when $page->outputFormatting is true.
*
*/
public function ___formatValue(Page $page, Field $field, $value) {
foreach ($value as $k => $v) {
if (!is_int($v))
continue;
$template = $this->fuel('templates')->get($v);
if ($template) {
$value[$k] = $template;
} else {
unset($value[$k]);
}
}
return $value;
}
public function getInputfield(Page $page, Field $field) {
$inputfield = $this->modules->get('InputfieldAsmSelect');
$inputfield->attr('name', $field->name);
if (!empty($field->templateTypes)) {
$selector = "id=";
foreach ($field->templateTypes as $templateType) {
$selector .= $templateType . '|';
}
$selector = rtrim($selector, '|');
$templates = $this->fuel('templates')->find($selector);
} else {
$templates = $this->fuel('templates');
}
foreach ($templates as $template) {
$id = $template->id;
$label = $template->name . ($template->label ? ' (' . $template->label . ')' : '');
$inputfield->addOption($id, $label);
}
return $inputfield;
}
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['data'] = 'int NOT NULL';
$schema['keys']['data'] = 'KEY data (data, pages_id, sort)';
return $schema;
}
public function ___getCompatibleFieldtypes(Field $field) {
$fieldtypes = new Fieldtypes();
return $fieldtypes;
}
public function ___getConfigInputfields(Field $field) {
$inputfields = parent::___getConfigInputfields($field);
$templateTypes = array();
$lastType = '';
$f = $this->modules->get("InputfieldCheckboxes");
$f->attr('name', 'templateTypes');
foreach ($this->templates as $template) {
if ($template->id == $lastType)
continue;
$f->addOption($template->id, $template->name . ($template->label ? ' (' . $template->label . ')' : ''));
$lastType = $template->id;
}
$f->attr('value', is_array($field->templateTypes) ? $field->templateTypes : array());
$f->label = "Template Types";
$f->description = "Check all of the templates that may be selectable in this field. If none are selected, then all types will be allowed.";
$inputfields->append($f);
return $inputfields;
}
}