-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectizeImageTags.module
More file actions
194 lines (152 loc) · 8 KB
/
SelectizeImageTags.module
File metadata and controls
194 lines (152 loc) · 8 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
<?php
/**
* SelectizeImageTags module for ProcessWire
*
*/
/**
* Class SelectizeImageTags
*/
class SelectizeImageTags extends WireData implements Module {
/**
* Upgrade
*/
public function ___upgrade($fromVersion, $toVersion) {
// Upgrade from < v0.0.3
if($fromVersion < 3) {
if(version_compare($this->config->version, '3.0.61', '<')) {
throw new WireException("The minimum required ProcessWire version is 3.0.61");
}
}
}
protected $jsConfig = array();
public function addProperty(HookEvent $event) {
$event->return = 0;
}
public function addPropertyString(HookEvent $event) {
$event->return = '';
}
public function init() {
$this->addHookProperty("InputfieldFile::selectizeTagField", $this, "addProperty");
$this->addHookProperty("InputfieldFile::selectizeTagList", $this, "addPropertyString");
$this->addHookProperty("InputfieldFile::selectizePlugins", $this, "addProperty");
$this->addHookProperty("InputfieldFile::selectizeAllowCreate", $this, "addProperty");
/**
* Adds additional options to the InputfieldText edit screen.
*
*/
$this->addHookAfter("InputfieldFile::getConfigInputfields", function($event) {
$that = $event->object;
// Enable Selectize.js for this Field?
$field = $this->modules->get('InputfieldCheckbox');
$field->attr('name', 'selectizeTagField');
$field->attr('value', 0);
$field->label = $this->_('Enable Selectized input for the tags field?');
$field->description = $this->_('If checked, you may setup selectable tags for the image tags field.');
//$field->showIf = 'useTags>0';
if($that->selectizeTagField) $field->attr('checked', 'checked');
else $field->collapsed = Inputfield::collapsedYes;
$event->return->append($field);
// list of selectable text strings for this Field
$field = $this->modules->get('InputfieldTextarea');
$field->setAttribute('name', 'selectizeTagList');
$field->label = $this->_('Selectable Tags');
$description = $this->_('List of tags, 1 per line: comma separated value,label.');
$field->description = $description;
$field->value = trim($that->selectizeTagList);
$field->showIf = 'selectizeTagField>0';
$event->return->append($field);
// Allow create ad-hoc tags
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'selectizeAllowCreate';
$field->label = __('Allow tag creation?', __FILE__);
$field->description = $this->_('By default tags are limited to the items listed above. Check this to allow any tags to be input.');
$field->notes = "If this is unchecked AND there are already tags on images AND those tags are not in your permitted list, they will end up being deleted when users save the page!";
$field->attr('value', 0);
if($that->selectizeAllowCreate) $field->attr('checked', 'checked');
$field->showIf = 'selectizeTagField>0';
$field->columnWidth = 50;
$event->return->append($field);
// Plugins to load
$field = $this->modules->get('InputfieldCheckboxes');
$field->name = 'selectizePlugins';
$field->label = __('Plugins', __FILE__);
$field->addOption('remove_button', __('Remove Button'));
$field->addOption('drag_drop', __('Drag and Drop'));
$field->addOption('restore_on_backspace', __('Restore on Backspace'));
$field->showIf = 'selectizeTagField>0';
$field->value = isset($that->selectizePlugins) ? $that->selectizePlugins : '';
$field->columnWidth = 50;
$event->return->append($field);
});
$this->addHookAfter('InputfieldFile::getConfigAllowContext', function($event) {
$array = $event->return;
$addon = array('selectizeTagField','selectizeTagList','selectizeAllowCreate','selectizePlugins');
$merged = array_merge($array, $addon);
$event->return = $merged;
});
/**
* Attaches additional JS & CSS files when editing a page that
* uses this field/module and add the attrs to the fields.
*
*/
$this->addHookBefore('InputfieldFile::renderReadyHook', function($event) {
$that = $event->object;
// Only load additional assets if:
// - Process is PageEdit
// - useTags is checked
// - selectizeTagField is enabled
// - selectizeTagList is populated for this field
if( ($that->process == 'ProcessPageEdit') && ($that->selectizeTagField && $that->selectizeTagList && $that->useTags) ) {
if(version_compare($this->config->version, '3.0.67', '<')) {
//$this->modules->JquerySelectize;
$that->modules->JquerySelectize;
} else {
// if the version is >= than 3.0.67, and jQuerySelectize module is installed,
// init the module, in case a skin is selected; the module will use the core
// selectize library anyway...
if($this->modules->get('JquerySelectize')) {
$this->modules->JquerySelectize;
} else {
// shouldn't be necessary to init the plugin because core should already load the assets if tags are enabled on the field
//$this->wire('modules')->get('JqueryUI')->use('selectize');
}
}
$that->config->scripts->add($that->config->urls->siteModules . __CLASS__ . '/' . __CLASS__ . '.js?v=' . time());
$that->config->styles->add($that->config->urls->siteModules . __CLASS__ . '/' . __CLASS__ . '.css?v=' . time());
if(isset($that->selectizeTagList) && $that->selectizeTagList !== '') {
// this could be replaced with additional info about this field..
$that->tagsFieldLabel = "Tags (selectized)";
$tagsArray = array();
// User strings
$selectizeTagList = explode("\n", $that->selectizeTagList);
foreach($selectizeTagList as $row) {
if(strpos($row, ',')) {
$parts = explode(',', $row);
$tagVal = trim($parts[0]);
$tagTxt = isset($parts[1]) ? trim($parts[1]) : trim($parts[0]);
} else {
$tagVal = trim($row);
$tagTxt = trim($row);
}
$tagsArray[] = array('value' => $tagVal, 'text' => $tagTxt);
}
$this->jsConfig[$that->id]['create'] = $that->selectizeAllowCreate ? true : false;
$this->jsConfig[$that->id]['delimiter'] = ' ';
$this->jsConfig[$that->id]['options'] = $tagsArray;
if(isset($that->selectizePlugins)) {
$this->jsConfig[$that->id]['plugins'] = $that->selectizePlugins;
}
$this->config->js(__CLASS__, $this->jsConfig);
if(version_compare($this->config->version, '3.0.66', '>')) {
// prevent core from initializing the field...
$this->addHookBefore("InputfieldFile::render", $this, "hookInputfieldFileRender_removeClass");
}
} // end if isset
} // end if process
}); // end add hook
} //end init()
public function hookInputfieldFileRender_removeClass(HookEvent $event) {
$object = $event->object;
$object->wrapClass = str_replace('InputfieldFileHasTags', '', $object->wrapClass);
}
} // end class