-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageListCustomSort.module.php
More file actions
180 lines (158 loc) · 5.81 KB
/
PageListCustomSort.module.php
File metadata and controls
180 lines (158 loc) · 5.81 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
<?php namespace ProcessWire;
/**
* Enables the use of a custom sort setting for children, using multiple properties
*
* Copyright (c) 2024 EPRC
* Licensed under MIT License, see LICENSE
*
*/
class PageListCustomSort extends WireData implements Module {
public function init() {
$this->addHookBefore("Pages::find", $this, "addCustomSortQuery");
$this->addHookAfter("ProcessPageEdit::buildFormChildren", $this, "addCustomOptionPage");
$this->addHookAfter("ProcessTemplate::buildEditForm", $this, "addCustomOptionTemplate");
$this->addHookAfter("ProcessPageEdit::processInput", $this, "saveCustomOptionPage");
$this->addHookBefore("ProcessTemplate::executeSave", $this, "saveCustomOptionTemplate");
}
public function addCustomSortQuery(HookEvent $event) {
$selector = (string) $event->arguments(0);
if(strpos($selector, "sort=_custom") === false) return;
preg_match("/(?:parent_id|has_parent)=(\d+),/", $selector, $matches);
if(count($matches) <= 1) return;
$page = $event->pages->get($matches[1]);
if(!$page->id) return;
$selector = preg_replace(
"/, ?sort=_custom/",
$this->getCustomSortSelector($page),
$selector
);
$event->arguments(0, new Selectors($selector));
}
public function addCustomOptionPage(HookEvent $event) {
/** @var Page $page */
$page = $event->object->getPage();
/** @var InputfieldWrapper $wrapper */
$wrapper = $event->return;
if($fieldset = $wrapper->getChildByName("ChildrenSortSettings")) {
$this->addCustomOption($fieldset, $this->getCustomSort($page));
} elseif($fieldset = $wrapper->getChildByName("ChildrenPageList")) {
if(strpos($fieldset->notes, "_custom") !== false) {
$fieldset->notes = str_replace("_custom", $page->template->sortfield_custom, $fieldset->notes);
}
}
}
public function addCustomOptionTemplate(HookEvent $event) {
/** @var Template $template */
$template = $event->arguments("template");
/** @var InputfieldForm $form */
$form = $event->return;
/** @var InputfieldFieldset $fieldset */
$fieldset = $form->getChildByName("sortfield_fieldset");
$this->addCustomOption($fieldset, $template->sortfield_custom);
}
private function addCustomOption(InputfieldFieldset $fieldset, $value = "") {
/** @var InputfieldSelect $sortfield */
$sortfield = $fieldset->getChildByName("sortfield");
$sortfield->insertOptions(["_custom" => $this->_("Custom")], "sort");
/** @var InputfieldCheckbox $f */
$sortfieldReverse = $fieldset->getChildByName("sortfield_reverse");
$sortfieldReverse->showIf = "sortfield!='', sortfield!=sort, sortfield!=_custom";
/** @var InputfieldText $f */
$f = $this->modules->get("InputfieldText");
$f->attr("id+name", "sortfield_custom");
$f->label = $this->_("Sort string");
$f->description = $this->_("To sort using multiple fields, type their name separated with a comma. You may also specify subproperties using `property.subproperty`. To reverse the sort of a field, prepend its name with a minus \"-\"");
$f->placeholder = "-created, name";
$f->showIf = "sortfield=_custom";
$f->val($value);
$fieldset->add($f);
}
private function getCustomSortSelector(Page $page) {
$fields = "";
if($page->template->sortfield === "_custom") {
$fields = $page->template->sortfield_custom ?? "";
} elseif($page->sortfield === "_custom") {
$fields = $this->getCustomSort($page);
}
$sort = "";
foreach(explode(",", $fields) as $field) {
if(!$field) continue;
$sort .= ",sort=" . trim($field);
}
return $sort;
}
private function getCustomSort(Page $page) {
$sortfield_custom = "";
$sql = "SELECT sortfield_custom " .
"FROM pages_sortfields " .
"WHERE pages_id=$page->id";
try {
$query = $this->wire()->database->prepare($sql);
$query->execute();
if($query->rowCount()) {
$sortfield_custom = $query->fetchColumn();
}
} catch(\Exception $e) {
$this->error($e->getMessage());
}
return $sortfield_custom;
}
public function saveCustomOptionPage(HookEvent $event) {
/** @var InputfieldWrapper $form */
$form = $event->arguments(0);
if(!$form->getChildByName("ChildrenSortSettings") || $event->arguments("level")) return;
/** @var Page $page */
$page = $event->object->getPage();
$this->addHookAfter("Pages::save(id=$page)", function(HookEvent $event) {
if($event->return) { // page is successfully saved
/** @var Page $page */
$page = $event->arguments("page");
$sortfield = $this->pages->sortfields()->encode($page->sortfield);
if($sortfield == 'sort' || !$sortfield) return;
$sortfield_custom = $event->input->post->selectorValue("sortfield_custom", [
"useQuotes" => false
]);
$sql = "UPDATE pages_sortfields " .
"SET sortfield_custom='$sortfield_custom' " .
"WHERE pages_id=$page->id";
try {
$this->wire()->database->exec($sql);
} catch(\Exception $e) {
$this->error($e->getMessage());
}
}
});
}
public function saveCustomOptionTemplate(HookEvent $event) {
/** @var WireInput $input */
$input = $event->input;
if(!$input->post("sortfield", ["_custom"])) return;
$id = (int) $input->post("id");
if(!$id) $id = (int) $input->get("id");
/** @var Template $template */
$template = $event->templates->get($id);
$sortfield_custom = $input->post->selectorValue("sortfield_custom", [
"useQuotes" => false
]);
$input->post->set('sortfield_reverse', 0);
$template->sortfield_custom = $sortfield_custom;
}
public function ___install() {
$sql = "ALTER TABLE pages_sortfields " .
"ADD sortfield_custom text NOT NULL";
try {
$this->wire()->database->exec($sql);
} catch(\Exception $e) {
$this->error($e->getMessage());
}
}
public function ___uninstall() {
$sql = "ALTER TABLE pages_sortfields " .
"DROP COLUMN sortfield_custom";
try {
$this->wire()->database->exec($sql);
} catch(\Exception $e) {
$this->error($e->getMessage());
}
}
}