-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessQuickToggle.module
247 lines (210 loc) · 8.29 KB
/
ProcessQuickToggle.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* ProcessQuickToggle
*
* Quickly toggle your checkboxes with extra action buttons via AJAX.
*
* @todo FieldtypeCheckbox does not necessarily have 1 or 0 value. It could be
* configured to have other values. Try to set values according to those
* settings.
*
*/
class ProcessQuickToggle extends Process implements Module {
const quickToggleCheck = 'quickToggleCheck';
const quickToggleUncheck = 'quickToggleUncheck';
protected $field = null;
public static function getModuleInfo()
{
return array(
'title' => 'Process Quick Toggle',
'version' => '0.1.1',
'summary' => _('Quickly toggle your checkboxes with extra action buttons via AJAX.'),
'requires' => array('ProcessPageList', 'InputfieldCheckbox', 'ProcessWire>=2.6.5'),
'autoload' => 'process=ProcessPageList|ProcessField',
);
}
public function init()
{
$this->addHookBefore('ProcessField::executeEdit', $this, 'hookExecuteEdit');
$this->addHookAfter('Inputfield::getConfigAllowContext', $this, 'hookConfigAllowContext');
$this->addHookAfter('InputfieldCheckbox::getConfigInputfields', $this, 'hookConfigInputfields');
$this->addHookAfter('ProcessPageListActions::getExtraActions', $this, 'hookExtraActions');
$this->addHookAfter('ProcessPageListActions::processAction', $this, 'hookProcesAction');
}
public function getDefaultData()
{
return array(
'enable_quick_toggle' => 0,
'button_enable' => 'check',
'button_disable' => 'uncheck',
'button_enabled' => 'Checked',
'button_disabled' => 'Unchecked'
);
}
protected function hookExecuteEdit(HookEvent $event)
{
$id = (integer) $this->input->get->id;
$fieldgroup_id = (integer) $this->input->get->fieldgroup_id;
if ($fieldgroup_id) $fieldgroup = wire('fieldgroups')->get($fieldgroup_id);
if ($fieldgroup_id && $fieldgroup instanceof FieldGroup) {
$this->field = $fieldgroup->getField($id, true);
} else {
$this->field = wire('fields')->get($id);
}
}
protected function hookConfigAllowContext(HookEvent $event)
{
$allowed = $event->return;
if (!isset($allowed['enable_quick_toggle'])) $allowed[] = 'enable_quick_toggle';
if (!isset($allowed['button_enable'])) $allowed[] = 'button_enable';
if (!isset($allowed['button_disable'])) $allowed[] = 'button_disable';
if (!isset($allowed['button_enabled'])) $allowed[] = 'button_enabled';
if (!isset($allowed['button_disabled'])) $allowed[] = 'button_disabled';
$event->return = $allowed;
}
protected function hookConfigInputfields(HookEvent $event)
{
$fields = $event->return;
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'enable_quick_toggle';
$field->label = $this->_('Enable Quick Toggle');
if ($this->field->enable_quick_toggle) $field->attr('checked', 1);
$fields->add($field);
$field = $this->modules->get('InputfieldText');
$field->name = 'button_enable';
$field->label = $this->_('Button Enable Text');
$field->description = $this->_("The text for the action button, to enable. \nE.g. `check`");
$field->notes = $this->_('If you want an icon instead of a text, prefix it with "`icon-`". E.g: `icon-chain`');
$field->attr('value', $this->field->button_enable);
$field->showIf = "enable_quick_toggle=1";
$field->columnWidth = 50;
$fields->add($field);
$field = $this->modules->get('InputfieldText');
$field->name = 'button_disable';
$field->label = $this->_('Button Disable Text');
$field->description = $this->_("The text for the action button, to disable. \nE.g. `uncheck`");
$field->notes = $this->_('If you want an icon instead of a text, prefix it with "`icon-`". E.g: `icon-chain-broken`');
$field->attr('value', $this->field->button_disable);
$field->showIf = "enable_quick_toggle=1";
$field->columnWidth = 50;
$fields->add($field);
$field = $this->modules->get('InputfieldText');
$field->name = 'button_enabled';
$field->label = $this->_('Message When Enabled');
$field->description = $this->_("The little message text that appears after the enable action. \nE.g. `Checked`");
$field->attr('value', $this->field->button_enabled);
$field->showIf = "enable_quick_toggle=1";
$field->columnWidth = 50;
$fields->add($field);
$field = $this->modules->get('InputfieldText');
$field->name = 'button_disabled';
$field->label = $this->_('Message When Disabled');
$field->description = $this->_("The little message text that appears after the disable action. \nE.g. `Unchecked`");
$field->attr('value', $this->field->button_disabled);
$field->showIf = "enable_quick_toggle=1";
$field->columnWidth = 50;
$fields->add($field);
$event->return = $fields;
}
protected function hookExtraActions(HookEvent $event)
{
$page = $event->arguments[0];
$extras = $event->return;
$adminUrl = $this->wire('config')->urls->admin . 'page/';
// If page is locked then no toggle buttons for you (^_^)
if ($page->isLocked()) return;
foreach ($page->fields as $field) {
$field = $page->fields->getField($field->id, true);
if (!$field->enable_quick_toggle) continue;
if (!$page->editable($field->name)) continue;
$labels = $this->getLabels($field);
if ($page->get($field->name)) {
$action = self::quickToggleUncheck;
$extras['quickToggleUncheck_' . $field->name] = array(
'cn' => 'Disable',
'name' => $labels['button_disable'],
'url' => "$adminUrl?action=$action&id=$page->id&fieldId=$field->id",
'ajax' => true,
);
} else {
$action = self::quickToggleCheck;
$extras['quickToggleCheck_' . $field->name] = array(
'cn' => 'Enable',
'name' => $labels['button_enable'],
'url' => "$adminUrl?action=$action&id=$page->id&fieldId=$field->id",
'ajax' => true,
);
}
}
$event->return = $extras;
}
protected function getLabels($field)
{
$defaults = $this->getDefaultData();
$labels = array();
foreach ($defaults as $key => $value) {
if ($field->$key) $labels[$key] = $this->formatLabel($field->$key);
else $labels[$key] = $value;
}
return $labels;
}
protected function formatLabel($label)
{
// If the string starts with `icon-` then it should be FontAwesome icon
if (strpos($label, 'icon-') === false) return $label;
else return wireIconMarkup($label);
}
public function hookProcesAction(HookEvent $event)
{
$page = $event->arguments[0];
$action = $event->arguments[1];
$result = array(
'action' => $action,
'success' => false,
'message' => 'noob',
'updateItem' => $page->id,
'remove' => false,
'refreshChildren' => 0
);
// If the action is not the one we care about then return
if ($action !== self::quickToggleCheck AND $action !== self::quickToggleUncheck) return;
// If the fieldId is not present then...
$fieldId = (integer) $this->input->get->fieldId;
if (!$fieldId) {
$result['message'] = $this->_("The fieldId is unknown.");
$event->return = $result;
return;
}
// If the field cannot be retrieved then...
$field = $page->fields->get($fieldId);
$field = $page->fields->getField($field->name, true);
if (!$field) {
$result['message'] = $this->_("The field with id: `$fieldId` couldn't be retrieved.");
$event->return = $result;
return;
}
// Check for permissions
if (!$page->editable($field->name) && !wire('user')->isSuperuser()) {
$result['message'] = $this->_("You haven't got permissions for this action.");
$event->return = $result;
return;
}
// Prepare default value and message
$labels = $this->getLabels($field);
if ($action === self::quickToggleCheck) {
$value = 1;
$message = $labels['button_enabled'];
} else {
$value = 0;
$message = $labels['button_disabled'];
}
try {
$result['success'] = $page->setAndSave($field->name, $value);
if ($result['success']) $result['message'] = $message;
else $result['message'] = $this->_("Something went wrong. We could not perform the task.");
} catch (Exception $e) {
$result['message'] = $e->getMessage();
}
$event->return = $result;
}
}